我正在尝试向用户集合添加一个附加字段。
我设置了正确的update权限。
//called in my template event handler for a form post
Meteor.users.update(Meteor.userId(), { $set: { company: company._id }});
但是,每当我访问 Meteor.user() 时,我都看不到公司字段?
我已经尝试设置一个 pub/sub,但我仍然没有任何运气来检索该字段。
有什么想法吗?
我正在尝试向用户集合添加一个附加字段。
我设置了正确的update权限。
//called in my template event handler for a form post
Meteor.users.update(Meteor.userId(), { $set: { company: company._id }});
但是,每当我访问 Meteor.user() 时,我都看不到公司字段?
我已经尝试设置一个 pub/sub,但我仍然没有任何运气来检索该字段。
有什么想法吗?
默认情况下,您只能从客户端更新您的个人资料(不是任意字段)。所以你可以这样做:
Meteor.users.update(Meteor.userId(), {$set: {'profile.company': company._id }});
无论如何,这可能是您想要做的。有关允许/拒绝规则和向客户端发布用户字段的更多信息,请仔细阅读文档的用户部分。
首先设置权限以允许用户在配置文件字段之外进行更新。
Meteor.users.allow({
update: function(userId, doc){
return doc._id === userId; // can update their own profile
}
});
然后设置公司字段的发布
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
{fields: {'company': 1}});
});
并订阅
Meteor.subscribe('userData');