Search code examples
meteorminimongo

How update an array, client side


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) ?


Solution

  • 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.