2017-02-05 141 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文件夹。