0

我有一个关于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);

我的自动表单如下所示:

{{> quickForm collection="Objects" id="insertTestForm" type="insert"}} 

我实际上希望我的 type 属性有一个选择选项字段,但是会出现一个文本输入。有谁知道为什么?

根据文档[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.
4

2 回答 2

0

由于没有人可以帮助我解决此事件,我想出了一个替代解决方案:

// 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由于它返回一个包含和属性的对象数组,因此valueautoform 将自动呈现一个选择选项。

于 2016-01-10T20:15:28.947 回答
0

您的类型不是文档所说的“Mongo.Collection 实例”;这是一个架构。尝试这个:

Schemas.Object = new SimpleSchema({
    type: {
        type: ObjectTypes,
        optional: true
    },
    ...
于 2016-01-08T19:50:44.463 回答