我有关于关系和autoform的collection2的问题。 我尝试实现1:n关系,其中每个对象只有1个objectType,而对于每个objectType,可以引用多个对象。
我的架构如下所示:
// register collections
Objects = new Mongo.Collection('objects');
ObjectTypes = new Mongo.Collection('objectTypes');
// define schema
var Schemas = {};
Schemas.ObjectType = new SimpleSchema({ // object type schema
name: {
type: String
}
});
Schemas.Object = new SimpleSchema({ // object schema
type: {
type: ObjectTypes.Schema,
optional: true
},
title: {
type: String
}
});
// attach schemas
ObjectTypes.attachSchema(Schemas.ObjectType);
Objects.attachSchema(Schemas.Object);
我的autoform看起来像这样:
{{> quickForm collection="Objects" id="insertTestForm" type="insert"}}
我实际上希望我的type属性有一个select选项字段,但会出现一个文本输入。谁知道为什么?
根据文档[1],它应该是一个选择选项字段:
If you use a field that has a type that is a Mongo.Collection instance, autoform will automatically provide select options based on _id and name fields from the related Mongo.Collection. You may override with your own options to use a field other than name or to show a limited subset of all documents. You can also use allowedValues to limit which _ids should be shown in the options list.
[1] https://github.com/aldeed/meteor-collection2/blob/master/RELATIONSHIPS.md#user-content-autoform
修改 如果我使用
type: ObjectTypes,
而不是
type: ObjectTypes.Schema,
我的应用崩溃,引发以下错误:
Your app is crashing. Here's the latest log.
/Users/XXX/.meteor/packages/meteor-tool/.1.1.3.ik16id++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
throw(ex);
^
RangeError: Maximum call stack size exceeded
Exited with code: 8
Your application is crashing. Waiting for file change.
答案 0 :(得分:0)
您的类型不是" Mongo.Collection实例"就像文件说的那样;它是一个架构。试试这个:
Schemas.Object = new SimpleSchema({
type: {
type: ObjectTypes,
optional: true
},
...
答案 1 :(得分:0)
由于没有人能帮助我解决这一事件,我提出了另一种解决方案:
a uid search uid 1:2
正如您所见,我将// register collections
Objects = new Mongo.Collection('objects');
ObjectTypes = new Mongo.Collection('objectTypes');
// define schema
var Schemas = {};
Schemas.Object = new SimpleSchema({ // object schema
type: {
type: String,
optional: true,
autoform: {
return ObjectTypes.find().map(function(c) {
return{label: c.name, value: c._id}
});
}
},
// ...
});
// attach schema
Objects.attachSchema(Schemas.Object);
集合中需要的属性手动映射到objectTypes
属性。由于它返回一个包含autoform
和label
属性的对象数组,因此autoform将自动呈现一个select选项。