我想在我的 meteor 应用程序中存储每个登录用户的信息,例如他们的个人资料图片、简历等。但是如果我尝试做一些像 Meteor.user().picLink = "...";它似乎在每次后续调用 Meteor.user() 时被删除。我认为这意味着我不应该像那样将额外数据直接存储在用户对象上。
对此,我能想到的唯一回应是拥有一个单独的集合,其中包含用户数据。但这似乎很难与 Meteor.users 保持一致。有没有更好的办法?
最佳答案
所有用户帐户都带有一个自动发布的个人资料
字段,您可以像这样更新它:
var userId = Meteor.userId();
var url = 'http://example.com/kittens.jpg';
Meteor.users.update(userId, {$set: {'profile.photo': url});
这将更新底层数据库并跨连接持久保存。
正如我指出的 here你应该知道配置文件对象当前默认是可编辑的,即使 insecure
包已被删除。这意味着任何用户都可以打开控制台并修改他/她的个人资料。
更好的方法是拒绝更新并改用方法:
客户端
var url = 'http://example.com/kittens.jpg';
Meteor.call('update.photo', url);
服务器
Meteor.users.deny({
update: function() {return true;}
});
Meteor.methods({
'update.photo': function(url) {
check(url, String);
Meteor.users.update(this.userId, {$set: {'profile.photo': url}});
}
});
关于meteor - 在 Meteor 中存储每个用户的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624626/