0
votes

I have an issue regarding collection2 with relationships and autoform. I try to implement an 1:n relationship, where each object has exactly 1 objectType, while to each objectType multiple objects can be referred to.

My schema looks as follows:

// 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);

My autoform looks like this:

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

I actually would expect a select option field for my type attribute, however, a text input appears. Anyone knows why?

According to the documentation [1], it should be a select option field:

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

EDIT If I use

type: ObjectTypes,

instead of

type: ObjectTypes.Schema,

my app crashes, throwing the following error:

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.
2

2 Answers

0
votes

Your type isn't "a Mongo.Collection instance" like the documentation says; it's a Schema. Try this:

Schemas.Object = new SimpleSchema({
    type: {
        type: ObjectTypes,
        optional: true
    },
    ...
0
votes

Since nobody could help me solve this incident, I came up with an alternate solution:

// 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);

As u can see, I manually map the attributes I need from the objectTypes collection into the autoform attribute. Since it returns an array of objects, containing the label and value attributes, autoform will automatically render a select option.