I am attempting to create a relationship between 2 collections, but one of the collections is not available to be referenced in the other. Specifically, I have 2 collections: Sites and ContentTypes. This is what they include:
// app/lib/collections/sites.js
Sites = new Mongo.Collection('sites');
Sites.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
max: 100
},
client: {
type: String,
label: "Client",
max: 100
},
created: {
type: Date,
autoValue: function() {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset(); // Prevent user from supplying their own value
}
}
}
}));
And here's the ContentTypes collection:
// app/lib/collections/content_types.js
ContentTypes = new Mongo.Collection('content_types');
ContentTypes.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
max: 100
},
machineName: {
type: String,
label: "Machine Name",
max: 100
},
site:{
type: Sites
},
created: {
type: Date,
autoValue: function() {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset(); // Prevent user from supplying their own value
}
}
}
}));
When I add the Sites reference to the ContentTypes schema, I the app crashes with the error:
ReferenceError: Sites is not defined at lib/collections/content_types.js:32:11
I haven't had much luck finding documentation for relationships in collection2 beyond this. It looks like the format referenced there is supposed to work based on this thread.
This is due to the order Meteor loads files. See section on File Load Order here:
There are several load ordering rules. They are applied sequentially to all applicable files in the application, in the priority given below:
- HTML template files are always loaded before everything else
- Files beginning with main. are loaded last
- Files inside any lib/ directory are loaded next
- Files with deeper paths are loaded next
- Files are then loaded in alphabetical order of the entire path
e.g. rename app/lib/collections/sites.js to app/lib/collections/a_sites.js and the Sites variable will be defined when the content_types.js file is being loaded.