Lukas Lukac
Lukas Lukac

Reputation: 8346

Meteor collection.update permisions

Hi i dont understand why is this not working?

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

I have update allow method as well

Notifications = new Meteor.Collection('Notifications');

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

Error appear:

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

Upvotes: 3

Views: 2821

Answers (2)

Sam Ballantyne
Sam Ballantyne

Reputation: 487

Just to update the above answer:

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 });

This is what you do if you need to update multiple fields. The underscore pluck method, used in tandem with a field specifier, makes sure that data isn't being trucked around unnecessarily.

All my best,

Sam

Upvotes: 1

Tarang
Tarang

Reputation: 75945

To update a collection you can only use the document's _id. So you need to query for it first

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

This is only for code that runs on the client. On the server you can run the code as you had it.

Upvotes: 6

Related Questions