Meteor collection.update权限

时间:2013-06-22 14:06:33

标签: collections meteor

嗨,我不明白为什么这不起作用?

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

我也有更新允许方法

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 个答案:

答案 0 :(得分:6)

要更新集合,您只能使用文档_id。所以你需要先查询它

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

这仅适用于在客户端上运行的代码。在服务器上,您可以像运行代码一样运行代码。

答案 1 :(得分: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 });

如果您需要更新多个字段,可以执行此操作。与字段说明符串联使用的下划线插入方法可确保数据不会被不必要地卡车化。

尽我所能,

萨姆