Meteor集合更新权限

3

你好,我不明白为什么这个东西不能正常工作?

Notifications.update({'userId':Meteor.userId(), 'notifyUserId':notifyFriendId}, {$set: {read: 1}});

我已经更新了allow方法。
Notifications = new Meteor.Collection('Notifications');

Notifications.allow({
  update: function(userId, doc) {
    return true;
  }
});

出现错误:

Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403] 
2个回答

6

要更新一个集合,您只能使用文档的_id。因此,您需要先查询它。

var docid = Notifications.findOne({'userId':Meteor.userId(), 'notifyUserId':notifyFriendId});
Notifications.update({_id:docid._id}, {$set: {read: 1}});

这只适用于在客户端运行的代码。在服务器上,您可以按照原来的方式运行代码。


很奇怪。顺便说一句:我通过Meteor.methods解决了它,但是很好奇为什么这不起作用。谢谢回答。我现在不会尝试,但希望你是对的。标记为答案。 - Lukas Lukac

1

仅为更新上面的答案:

var documentIdentifiers = _.pluck(Documents.find({ param: 'yourParam'}, { fields: { _id: 1 }}).fetch(), '_id');
for (var i = 0; i < documentIdentifiers.length; i++)
  Documents.update(documentIdentifiers[i], { $do: whatever });

如果您需要更新多个字段,这是您应该采取的方法。使用与字段说明符配合使用的下划线pluck方法可以确保不必要地传输数据。

祝一切顺利,

山姆


谢谢。在我的情况下它有效。我使用一个断开连接的客户端。 - Victor Chigne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接