我试图以递归方式多次填充自引用模型。这是我的架构:
var TestSchema = new Schema({
title: { type: String },
counter: { type: Number },
children: [ { type: Schema.Types.ObjectId, ref: 'test' } ]
});
var models = {
Test: mongoose.model('test', TestSchema)
};
到目前为止,此功能代码将所有内容填充到一个级别:
models.Test.find().populate('children').exec(function(err, doc) {
if (err)
res.send(err);
else {
res.send(doc);
} });
但是当我尝试做类似的事情时:
models.Test.find().populate('children').populate('children.children').exec(function(err, doc) {
甚至:
models.Test.find().populate('children').exec(function(err, doc) {
if (err)
res.send(err);
else {
models.Test.populate(doc, 'children.children', function(err, doc) {
res.send(doc);
});
}
});
我收到此错误:
TypeError: Cannot call method 'path' of undefined
at search (..../api/node_modules/mongoose/lib/model.js:2059:28)
at search (..../api/node_modules/mongoose/lib/model.js:2078:22)
at Function._getSchema (..../api/node_modules/mongoose/lib/model.js:2085:5)
at populate (..../api/node_modules/mongoose/lib/model.js:1706:22)
at Function.Model.populate (..../api/node_modules/mongoose/lib/model.js:1686:5)
at Promise.<anonymous> (..../api/api.js:22:19)
at Promise.<anonymous> (..../api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (..../api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (..../api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
猫鼬3.6 发行说明说允许使用 Model.populate 进行深度填充,但这给了我一个错误。有谁知道发生了什么?