如何使用对象填充 Meteor 自动表单选项

How to populate Meteor autoform options with objects

我为集合定义了一个简单的模式:

testReport = new SimpleSchema({
  name : {
    type: String,
    label: "Your Name"   

  },
  school : {
    type: Object,
    label: "Your School"
  },
  "school.$.name" : {
    type: String
  },
  "school.$.region" : {
    type: String
  }

}, { tracker: Tracker });

Reports.attachSchema(testReport);

此架构通过自动表单使用以生成保存到报表集合的非常简单的表单:

   {{#autoForm collection=Collections.Reports id="form" type="insert"}}
        {{> afQuickField name="name" }}
        {{> afQuickField name="school" options=schoolOptions}}
        <input type="submit" value="Submit">
   {{/autoForm}}

一个简单的助手用于使用集合中的数据自动填充选项下拉列表:

schoolOptions: function () {
    if (Meteor.userId()) {
      Meteor.user().profile.schools)
            return Meteor.user().profile.schools.map(function (c) {
                return {label: c.name, value: JSON.stringify({ name: c.name, region: c.region})};
      });
    }
  }

一切都渲染成功,但是当我点击提交时,我在 School 字段上收到一个错误,上面写着

"Your School must be of type Object"

我确实尝试了几次重构,但没有任何效果。通常在选项下拉列表的 "value" 字段中,我们只放一个字符串、数字等。但我想在这里传递一个对象(由助手返回) 对象看起来像。

{"name":"someSchoolName","region":"someRegion"}

所以插入到 Collection 中的最终文档应该如下所示:

{  
  "name": "John",
  "school": {"name":"someSchoolName","region":"someRegion"}
}

有人可以帮忙吗? 非常感谢!

在您的架构中,更改此

"school.$.name" : { type: String},"school.$.region" : {type: String}

"school.name" : {type:String},"school.region" : {type: String}