更新Meteor.users

Nat*_*han 9 meteor

我已经为用户创建了一个表单来更新他们的个人资料.当我提交表单时,我收到了[403]错误.

Not permitted. Untrusted code may only update documents by ID.

我的问题是,如果我要使用Meteor.users.allow,在哪 - 文件/目录中 - 我会编写这段代码吗?

谢谢,内森

Aks*_*hat 17

您获得的错误不是您的允许/拒绝规则的结果.如果是这样,你会得到一个直接的"拒绝访问"错误.

在更新用户时(以及具有正确的allow规则),您需要更新用户_id- 特别是如果他们在客户端更新.

而不是

Meteor.users.update({name: "etc"}, {$set:..});
Run Code Online (Sandbox Code Playgroud)

您需要将其拆分为两个,一个用于获取_id,然后一个用于更新文档.

var user = Meteor.users.findOne({name: 'etc'});

Meteor.users.update({_id: user._id}, {$set:..});
Run Code Online (Sandbox Code Playgroud)

该规则在客户端上,您只能_id在更新时用于查找文档.

  • 你不需要显式的`Meteor.users.findOne();`在这里调用.`Meteor.users.update({_ id:Meteor.userId()},{$ set:{'profile.online':true}});`就足够了,因为你可以立即调用`Meteor.userId()`. (10认同)