无法访问Meteor.user()属性

时间:2017-02-05 21:04:36

标签: meteor typescript

我已经安装了Meteor手机身份验证包mys:accounts-phone,它应该在用户集合中添加phone.number子域。我尝试按如下方式访问此字段:

Meteor.user().phone.number

但打字稿显示错误

  

“用户”类型中不存在“手机”属性。

另一方面,我在users.profile中有自定义道具,并且可以通过这种方式轻松访问它们。 尚未删除不安全保护。自动发布已开启。

2 个答案:

答案 0 :(得分: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.module.ts文件中

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

@NgModule({
    imports: [
    ... other modules here
    AccountsModule
  ],
希望这会奏效。如果这不起作用,让我知道我会告诉你另一个解决方案

答案 1 :(得分:0)

来自Meteor 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文件夹中的文件中。