Nil*_*hal 8 collections publish-subscribe mongodb meteor meteor-accounts
我试过寻找一个答案,但似乎无法让这个工作.我正在使用Meteor与Cordova构建移动应用程序.
我想为我的Users集合添加一个属性(Meteor在我登录时创建的属性).即.例如,我想将{currentHotel:"Something"}添加到我的db.Users集合中.
我正在尝试以正确的Publish - Subscribe方式执行此操作.使用Meteor.methods被引用对实时应用程序不利.无论哪种方式,我想了解如何使用Publish - Subscribe更新Users集合.
//On the server, I did
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
{fields:{services: 1, currentHotel: 1}});
});
Run Code Online (Sandbox Code Playgroud)
因此,客户端应该可以访问currentHotel字段.现在更新"currentHotel"字段:
//On the client I do
Meteor.subscribe('userData');
var user = Meteor.users.findOne({"_id": Meteor.userId()});
Meteor.users.update({_id: user._id}, {$set:{currentHotel:'something'}});
//I tried this too
//Meteor.users.update({ _id: Meteor.userId() }, {$set: });
Run Code Online (Sandbox Code Playgroud)
在浏览器控制台上,我可以看到"currentHotel"和"services"就好了,这意味着发布 - 订阅工作正常.但我似乎无法更新currentHotel.我得到一个拒绝访问.为什么是这样?
此外,如果Collection中根本不存在"currentHotel"属性,我如何使用类似的发布 - 订阅添加它?我是否可以发布不存在的属性并允许客户订阅并添加该属性?
我提到了Meteor文档,这个,这个和这个,但似乎仍然无法让它工作!:-(
提前致谢!
您不应该更改用户对象的根字段:
Meteor.users.update(user, {$set: {"profile.currentHotel": "something"}});
Run Code Online (Sandbox Code Playgroud)
在这里阅读更多内容:http://docs.meteor.com/#/full/meteor_users
编辑:这个答案变得无关紧要,如最新文档中所示:https://guide.meteor.com/accounts.html#dont-use-profile
这个答案已超过1年了:)事实证明,强制在"个人资料"字段下编写用户特定字段作为一些严重的安全隐患(我一直认为),因为它允许客户修改这些子-fields.所以,是的,你可以在根对象中设置字段,但要注意不应该被客户端修改的字段应该在没有写权限的字段下(否则它会遇到同样的问题).