无法访问 Meteor.user() 属性



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

Meteor.user().phone.number

但是打字稿显示错误

属性"电话"在类型"用户"上不存在。

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

当我们

的角度组件初始化但我们的 meteor 数据无法从服务器访问时,就会发生这种情况。尝试使用用户注入代替 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

和在应用程序模块.ts文件中

  import { AccountsModule } from 'angular2-meteor-accounts-ui';
@NgModule({
    imports: [
    ... other modules here
    AccountsModule
  ],

希望这会起作用。 如果这不起作用,请告诉我,我会告诉您另一种解决方案

从流星文档中得到了可能的答案。

它解释了为什么会出现用户名属性。默认情况下,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文件中。

最新更新