我正在尝试创建两个集合之间的关系,但其中一个集合不可用于在另一个集合中引用。具体来说,我有2个集合:站点和内容类型。以下是它们的内容:
// 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
}
}
}
}));
这里是ContentTypes集合:
// 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
}
}
}
}));
当我添加站点引用到ContentTypes模式,我应用程序崩溃的错误:
ReferenceError: Sites is not defined在lib/收藏/content_types.js: 32:11
除此之外,我还没有找到关于collection2中关系的文档。看起来这里引用的格式应该是基于这个线程的
这是由于Meteor加载文件的顺序。
文件加载顺序有几个加载排序规则。它们按顺序应用于应用程序中所有可应用的文件,优先级如下:
- HTML模板文件总是在其他文件之前加载
- 以main开头的文件最后加载
- 任何lib/目录中的文件都将在下一个加载
- 路径更深的文件在
下加载- 然后按整个路径的字母顺序加载文件
。将app/lib/collections/Sites .js重命名为app/lib/collections/a_sites.js,并在加载content_types.js文件时定义Sites变量。