【问题标题】:Can't update Array of objects无法更新对象数组
【发布时间】:2017-06-13 02:19:57
【问题描述】:

我正在尝试使用简单模式创建一个对象数组。在此示例中,一个人有一个由各个职位填充的 careerHistory 对象。我不知道如何插入和更新对象数组。它只是错误。我可以让它工作的唯一方法是明确的,例如。 'careerHistory.position.1.company'.

我正在使用:

  • 流星
  • 简单模式
  • reactjs
  • collection2-core

路径:mongodb

const ProfileCandidateSchema = new SimpleSchema({
  userId: {
    type: String,
    regEx: SimpleSchema.RegEx.Id
  },
  careerHistory: { type: Array, optional: true },
  'careerHistory.position': { type: Object, optional: true },
  'careerHistory.position.$': { type: Object, optional: true },
  'careerHistory.position.$.company': { type: String, optional: true },
  'careerHistory.position.$.title': { type: String, optional: true }
});

路径:updateForm.js

ProfileCandidate.update(id, { $set: {
    'careerHistory.position.company': this.state['position.company'],
    'careerHistory.position.title': this.state['position.title'],
  }
});

【问题讨论】:

    标签: meteor simple-schema


    【解决方案1】:

    如果你想将对象推送到数组中

    ProfileCandidate.update(id, 
      { $push: { careerHistory: { position: {
        'company': this.state['position.company'],
        'title': this.state['position.title'],
      }}}
    });
    

    如果你想更新特定对象

    ProfileCandidate.update({ _id: id, 'careerHistory.position.company': this.state['position.company'] }, { $set: {
        'careerHistory.position.$.title': this.state['position.title'],
      }
    });
    

    在集合中查看$

    【讨论】:

    • 当我尝试在方法中运行push object 时,我收到错误Exception while invoking method 'careerHistory.update' Error: After filtering out keys not in the schema, your modifier is now empty
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 2021-11-28
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2019-06-06
    • 2021-07-08
    相关资源
    最近更新 更多