1

I tried to update an array in a collection by doing

Configs.update({_id:this.parent._id, "cles.cle":this.context.cle},
{$set: {"cles.$.alias": $(e.target).val()}});

but I got this error

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

How can I update data in an array, client side (minimongo) ?

Community
  • 1
  • 1
DavidFM
  • 193
  • 7

2 Answers2

1

When you call collection.update on the client, the selector may point out docs by their _id field only. So you must do something like collection.update({_id: "your id"}, <updates>}) (or by using the alternative _id selector: collection.update("your id", <updates>})).

Peppe L-G
  • 7,351
  • 2
  • 25
  • 50
  • ok, but how update , with only a _id and no index of the key of array where i must update my data? i miss something? So perhaps i can do a find before? but nothing better? – DavidFM Mar 21 '15 at 10:07
  • Use the selector `{_id:this.parent._id}` instead of `{_id:this.parent._id,"cles.cle":this.context.cle}`. – Peppe L-G Mar 21 '15 at 10:13
  • thanks for your awnser but i want to update only this: "cles.cle":this.context.cle and not all key "cles.cle" ... – DavidFM Mar 21 '15 at 10:28
  • I'm sorry, I don't understand what you mean. – Peppe L-G Mar 21 '15 at 16:31
1

OK, I think I finally understand the basis of your question. It looks like Meteor is treating your subselection of the array element in your selection criteria as an attempt to circumvent the policy of only allowing individual record updates. This feels like a bug in Meteor.

A possible work around:

  1. var cles = Configs.findOne({_id:this.parent._id}).cles;
  2. Modify the cles array as desired
  3. Update the whole array in place Configs.update({_id:this.parent._id},{$set: {cles: cles});

Feels clumsy. Another solution would be to create a server method to do this on the server side. This would be better if your array can be large.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39