2017-02-05 138 views
2

我已經安裝了Meteor手機驗證包mys:accounts-phone,它應該將phone.number子字段添加到用戶集合中。我嘗試如下訪問此領域:無法訪問Meteor.user()屬性

Meteor.user().phone.number 

但打字稿顯示錯誤

地產「電話」的類型「用戶」不存在。

另一方面,我在users.profile中有自定義的道具,並且可以用這種方式輕鬆訪問它們。 不安全尚未刪除。自動發佈已打開。

+0

有沒有辦法打印或以其他方式查看對象Meteor.user()'下的所有鍵? – theonlygusti

+0

{ \t「_id」:「xk67327YLKYeiPnKE」, \t「createdAt」:ISODate(「2017-01-19T12:21:55。692Z「), \t 」服務「:{ \t \t 」手機「:{}, \t \t 」簡歷「:{ \t \t \t 」loginTokens「: \t \t \t \t { \t \t \t \t \t 「when」:ISODate(「2017-01-19T12:22:05.008Z」), \t \t \t \t \t「hashedToken」:「CLBi1T8fthuGOqpiS2alZw7kaGm oiAYiCC4nJZoTssc = 「 \t \t \t \t}] \t \t} \t}, \t 」電話「:{ \t \t 」號「: 」9721234567「, \t \t 」驗證「:真}, \t」 profile「:{ \t \t」name「:」Lф11「, \t \t」regNum「:」Х205НА77「}} – Dmitry

+0

這是從DB中提取的。其實我不知道如何獲得Meteor.user()對象鍵。另一個奇怪的事情,通過Ctrl-Space Idea顯示用戶名屬性,這在數據庫中不存在。 – Dmitry

回答

0

當我們的角度組件被初始化但我們的流星數據沒有從服務器到達時,會發生這種情況。 嘗試使用用戶注射到位Meteor.user()

import {Component} from "@angular/core"; 
import { InjectUser } from 'angular2-meteor-accounts-ui';//<--**** import this**** 

@Component({ 
     selector: "login-buttons", 
     template 
    }) 
@InjectUser('user') //<--*** add this*** 
export class LoginButtonsComponent { 
     user: Meteor.User; //<--*** add this *** 
     constructor(private router: Router) {} 
} 

目前在用戶變量,你將有Meteor.User

的所有值,如果你想在HTML部分使用這種

打印
<div *ngIf="user"> 
    {{user.phone.number}} 
</div> 

不要忘記安裝

meteor add accounts-password 

meteor npm install --save angular2-meteor-accounts-ui 

和app.mod ule.ts文件

import { AccountsModule } from 'angular2-meteor-accounts-ui'; 

@NgModule({ 
    imports: [ 
    ... other modules here 
    AccountsModule 
    ], 

希望這會起作用。如果這不起作用讓我知道我會告訴你一個其他的解決方案

+0

我使用的是賬戶電話軟件包,所以不需要賬戶密碼,對不對?此外,沒有任何關於angular2-meteor-accounts-ui的描述或文件。我正在使用Ionic 2前端。 – Dmitry

+0

編譯時出現同樣的錯誤。 – Dmitry

0

從流星docs有可能的答案。

它解釋了爲什麼會出現用戶名屬性。默認情況下,Meteor只發布一些被認爲公開的字段。爲了暴露任何額外的領域,他們必須明確發佈。

還沒有時間測試,但認爲它應該工作。

另一個原因是,當發佈代碼沒有在服務器端執行時,使用相同的思想。試着把

Meteor.startup(() => { 
    // code to run on server at startup 
    Meteor.publish('userData', function() { 
     if(!this.userId) return null; 
     return Meteor.users.find(this.userId 
      //, {fields: {lastname: 1,}} 
     ); 
    }); 
}); 

在您的應用程序的文件夾內的/server文件夾。