【问题标题】:Meteor collection.update permisionsMeteor collection.update 权限
【发布时间】:2014-01-10 06:47:33
【问题描述】:

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

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] 

【问题讨论】:

    标签: collections meteor


    【解决方案1】:

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

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

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

    【讨论】:

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

    只是为了更新上面的答案:

    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 方法与字段说明符一起使用,可确保数据不会被不必要地运送。

    尽我所能,

    山姆

    【讨论】:

    • 谢谢。它适用于我的情况。我使用断开连接的客户端
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2017-05-11
    • 1970-01-01
    • 2020-11-08
    • 2016-11-30
    • 2015-02-20
    相关资源
    最近更新 更多