【发布时间】:2017-04-28 02:22:46
【问题描述】:
我为集合定义了一个简单的架构:
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 用于生成保存到 Reports 集合的非常简单的表单:
{{#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})};
});
}
}
一切都成功渲染,但是当我点击提交时,我在学校字段上收到一个错误,上面写着
“你的学校必须是 Object 类型”
我确实尝试了几次重构,但没有任何效果。通常在选项下拉列表的“值”字段中,我们只放置一个字符串、数字等。但我想在这里传递一个对象(由助手返回) 对象的样子。
{"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} -
@abk 这是正确的答案。谢谢你的努力!请将此作为答案而不是评论,以便我标记它。
标签: javascript meteor meteor-autoform simple-schema meteor-collection2