Arcy
Arcy

Reputation: 375

Update nested array in Meteor

This is my schema:

{
_id: "FJwSEMdDriddXLKXh"
name: "t"
number: "5"
owners: [
          {
           _id: 1,
           name: "Name",
           address: "Address",
           type: "Type",
           gender: "Gender",
           notes: []
          }
    ]
}

and on click I would add fields inside owners nested notes array. This is my Meteor template events:

Template.owners.event({
  'click #addNoteToOwner' : function(event, template){
      event.preventDefault();
      Territories.update({_id: template.data._id, owners: this._id}, {$push : {'owners.$.notes':  {title:"First Title"}}})
    }
})

If I try to update the doc, the following console errorT appear:

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

Is my syntax correct? How can I update this nested array?

Thanks!

Upvotes: 0

Views: 1051

Answers (1)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

There is mistake in your query in owners field:

Territories.update({
         _id: template.data._id,
         owners: {
             $elemMatch: {
                 _id: this._id
             }
         },
         {
             $push: {
                 'owners.$.notes': {
                     title: "First Title"
                 }
             }
         })

You won't be able to update directly from client side, because you use owners field and only _id is allowed. To solve this you can update using Meteor.methods and call that method from client side.

Upvotes: 3

Related Questions