简体   繁体   English

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

[英]Meteor.user().profile returns 'profile' undefined

I have a helper called 'isActive' and a template named 'create'.. see below 我有一个名为'isActive'的帮助器和一个名为'create'的模板..见下文

Template.create.isActive = function () {
  return Meteor.user().profile.isActive;
};

When I try to run this code it returns the following in console: "Exception in template helper: TypeError: Cannot read property 'profile' of undefined". 当我尝试运行此代码时,它在控制台中返回以下内容:“模板助手中的异常:TypeError:无法读取未定义的属性'profile'”。

Basically I want to pull the 'isActive' info from the current user profile and return it to the template. 基本上我想从当前用户配置文件中提取'isActive'信息并将其返回到模板。 Any idea why this does not work? 知道为什么这不起作用吗?

Update 更新

//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();
    }
  }
});

Meteor.user() is undefined. Meteor.user()未定义。 You are either not logged in with any user or your template is rendered before the users collection is synced to the client. 您未使用任何用户登录,或者在将用户集合同步到客户端之前呈现模板。 If you use a router like iron router you can wait for the collection being available or for the user being logged on. 如果您使用像铁路由器这样的路由器,您可以等待可用的收集或用户登录。

Without using a router the easiest would be to check if the user is defined: 不使用路由器,最简单的方法是检查用户是否已定义:

Template.create.isActive = function () {
    return Meteor.user() && Meteor.user().profile.isActive;
}

Since Meteor.user() is reactive the template will be rerendered when the user changes, ie when it is available. 由于Meteor.user()具有反应性,因此当用户更改时(即可用时)将重新呈现模板。

This is a common error btw, see also: 这是一个常见的错误,请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM