Meteor.user().profile返回'profile'未定义

Jas*_*ung 5 javascript meteor

我有一个名为'isActive'的帮助器和一个名为'create'的模板..见下文

Template.create.isActive = function () {
  return Meteor.user().profile.isActive;
};
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此代码时,它在控制台中返回以下内容:"模板助手中的异常:TypeError:无法读取未定义的属性'profile'".

基本上我想从当前用户配置文件中提取'isActive'信息并将其返回到模板.知道为什么这不起作用吗?

更新

//startup on server side:
Meteor.publish("userData", function() {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
      {fields: {'profile.isActive': 1}});
  } else {
    this.ready();
  }
});

//startup on client side
Meteor.subscribe('userData');

//router
this.route('list', {
  path: 'list',
  waitOn : function () {
    return Meteor.subscribe('userData');
  },
  data : function () {
    return Meteor.users.findOne({_id: this.params._id});
  },
  action : function () {
    if (this.ready()) {
      this.render();
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

Flo*_*ian 9

Meteor.user()未定义.您未使用任何用户登录,或者在将用户集合同步到客户端之前呈现模板.如果您使用像铁路由器这样的路由器,您可以等待可用的收集或用户登录.

不使用路由器,最简单的方法是检查用户是否已定义:

Template.create.isActive = function () {
    return Meteor.user() && Meteor.user().profile.isActive;
}
Run Code Online (Sandbox Code Playgroud)

由于Meteor.user()具有反应性,因此当用户更改时(即可用时)将重新呈现模板.

这是一个常见的错误,请参阅: