【发布时间】:2015-12-18 14:49:17
【问题描述】:
我正在尝试使用 mongoose js 嵌套模式,特别是 same 模式,以创建树状结构。在此配置中,一个文档只能有 1 个父级,但同一个文档可以是多个子级的父级。以下是我最初的处理方式:
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)
};
但是,当我运行它时,我得到了
A JavaScript error occurred in the main process
Uncaught Exception:
TypeError: Undefined type "undefined" at "file.required"
Did you try nesting Schemas? You can only nest using refs or arrays.
我一定是在接近这个错误。如何使用 mongoose js 嵌套相同的模式?
【问题讨论】:
-
请注意,在
module.exports定义中调用mongoose.model()时,您在代码示例中创建了两次模型。
标签: javascript node.js mongodb