尝试在 Meteor 中使用带有 Schema 的 autoForm 更新文档时出错

Error trying to update document using autoForm with Schema in Meteor

错误信息:

"Uncaught Error: After filtering out keys not in the schema, your modifier is now empty"

在 Meteor 中将自动表单与 collection2 和简单模式结合使用。 架构:

Injuries = new Mongo.Collection('injuries');

Rehab = new SimpleSchema({
  exercise: {
    type: String,
    label: "Rehab Exercise"
  },
  sets: {
    type: Number,
    label: "Sets"
  },
  duration: {
    type: Number,
    label: "Set Duration (in Minutes)"
  },
  date: {
    type: String,
    label: "Date of Rehab Exercise"
  },
  rehabnotes: {
    type: String,
    label: "Notes: i.e. 70% Intensity During Sprints",
    max: 200
  },
  injuryid:{
    type: String,
  }
});

Injuries.attachSchema(new SimpleSchema({
  player: {
    type: String,
    label: "Player",
    max: 50
  },
  injury: {
    type: String,
    label: "Injury"
  },
  notes: {
    type: String,
    label: "Notes",
    max: 200
  },
  injurydate: {
    type: Date,
    label: "Date of Injury",
  },
  rehab: {
    type: [Rehab],
    optional: true
  }
}));

以及模板中的表单代码:

 {{#autoForm collection="Injuries" schema="Rehab" id="insertRehabForm" type="update"}}
          <fieldset>

                {{> afQuickField name='exercise' options=options}}
                {{> afQuickField name='sets'}}
                {{> afQuickField name='duration'}}
                {{> afQuickField name='date'}}
                {{> afQuickField name='rehabnotes' rows=6}}

          </fieldset>
           <button type="submit" class="btn btn-primary">Insert</button>
                {{/autoForm}}

我可以在主页上使用自动表格很好地插入文档,在单个文档页面上使用此自定义表格我在提交时收到错误消息。

我在提交之前设置了一个收集挂钩,但这看起来只是一个架构错误,也许我在原始 Injuries 架构上设置的 Rehab 数组是搞砸了?我为此所做的搜索都是关于架构中的 "Type" 参数与预期不匹配,但我已经在此处检查过它们,它们看起来不错。建议?

基于AutoForm的docs:如果未设置collection属性,则需要schema属性,但是,即使设置了collection,AutoForm仍将使用提供的 schema 属性 generate (仅适用于 QuickForm)和 validate 表单(适用于 AutoForm 和 QuickForm)。

你的情况是,由于提供了两个属性(schemacollection),AutoForm 首先根据 Rehab 模式验证表单字段,当它成功时,它 尝试 将这些字段(运动、组、持续时间、日期、康复记录)的值插入到您的 Injuries 集合中,该集合在其自己的架构中没有这些键(它只有球员、受伤、笔记、受伤日期和 rehab).

根据您的要求,将 AutoForm 类型设置为 update-pushArray 似乎是最佳解决方案。检查 docs and the example 的用法。