我正在尝试使用mongoose js嵌套模式,特别是使用相同模式来创建类似树形结构的结构。在这种配置下,一个文档只能有一个父项,但同一个文档可以是多个子项的父项。以下是我最初的方法:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var mySchema = new Schema({
_id: {
type: ObjectId,
required: true
},
parent: {
_id: {
type: ObjectId,
ref: 'myModel'
},
required: true
}
});
var myModel = mongoose.model('myModel', mySchema);
module.exports = {
myModel: mongoose.model('myModel', myModel)
};
然而,当我运行这段代码时,出现了以下错误:
主进程中发生JavaScript错误
未捕获的异常:
TypeError: 在文件.required处出现未定义类型"undefined"
您是否尝试使用嵌套模式?您只能使用refs或数组进行嵌套。
我一定是做错了什么。在mongoose js中如何嵌套相同的模式?
module.exports
定义内部调用mongoose.model()
时,您正在创建模型两次。 - sshow