Meteor.user()对象

时间:2015-12-15 18:19:47

标签: meteor

MeteorJS。我对保存用户数据(登录用户)的最佳做法感兴趣。 UserId,CompanyId等(数据不容易改变)?会话var不是一个选项,因为数据不会生存下来"页面刷新。

我经常需要检查Meteor.user()。profile.companyId,并且常常出现问题,着名的"无法读取属性' profile'未定义"。

2 个答案:

答案 0 :(得分:1)

保持用户数据附加到用户的文档(在profile下或根文档上)通常是最佳做法。

我想你可以为guard数据定义一个全局帮助器。在这里,我将添加一个名为getPP的函数(获取配置文件属性),我将其添加到Meteor.users

_.extend(Meteor.users({
  getPP: function(key) {
    var user = Meteor.user();
    return user && user.profile && user.profile[key];
  }
}));

然后你可以像这样使用它:

Meteor.users.getPP('companyId');

它将返回profile属性的值或undefined并且不会抛出错误。

答案 1 :(得分:0)

您可以使用持久会话包:http://api.jquery.com/slice/

缺点是你正在使用全局命名空间,这是不受欢迎的。如果你限制它就没关系!