更新流星嵌套数组 [英] Update nested array in Meteor

查看:145
本文介绍了更新流星嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的架构:

{
_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"}}})
    }
})

如果我尝试更新文档,以下控制台errorT出现:

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?

谢谢!

推荐答案

有错误是在查询中所有者字段:

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

您将不能直接从客户端进行更新,因为您使用所有者字段只有 _id 被允许。为了解决这个问题,你可以使用更新 Meteor.methods 并调用该方法从客户端。

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.

这篇关于更新流星嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆