【问题标题】:Error trying to update document using autoForm with Schema in Meteor尝试在 Meteor 中使用带有 Schema 的 autoForm 更新文档时出错
【发布时间】:2016-03-02 12:56:21
【问题描述】:

错误信息:

“未捕获的错误:过滤掉不在模式中的键后,您的修饰符现在为空”

在 Meteor 中将 autoform 与 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 数组搞砸了?我为此所做的搜索都是关于架构中的“类型”参数与预期的不匹配,但我在这里检查了这些,它们看起来不错。有什么建议吗?

【问题讨论】:

    标签: mongodb meteor simple-schema


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-22
      • 2016-01-09
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多