猫鼬-自引用深度填充错误
Mongoose - Self-referential deep populate error
我试图递归地填充自我引用模型几次。下面是我的schema:
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)
mongoose 3.6发行说明说允许使用Model进行深度填充。填充,但这会给我一个错误。有人知道发生了什么吗?
模型的Mongoose文档。Populate方法声明第二个参数应该是一个选项对象,而不是字符串。
下面是他们提供的例子:
User.findById(id, function (err, user) {
var opts = [
{ path: 'company', match: { x: 1 }, select: 'name' }
, { path: 'notes', options: { limit: 10 }, model: 'override' }
]
User.populate(user, opts, function (err, user) {
console.log(user);
})
})
所以你的应该看起来像:
models.Test.find().populate('children').exec(function(err, doc) {
if (err)
res.send(err);
else {
var opts = [
{ path: 'children.children' }
];
models.Test.populate(doc, opts, function(err, doc) {
res.send(doc);
});
}
});
相关文章:
- 类型错误:url未定义extjs 4正在填充组合框
- ObjectId数组将不会在没有错误的情况下填充
- 使用 JS 填充 HTML 表,数据顺序错误
- 用来自 Javascript 的错误填充 JSP 中的错误字段
- 使用 PHP 填充引导输入 - 字符串变量中出现空格错误
- 尝试在不可扩展对象上定义属性时,弱映射填充项引发错误
- 填充隐藏字段Microsoft JScript 运行时错误:对象不支持此属性或方法
- Jquery 移动导航栏填充错误:太窄
- 填充导致错误的按钮启动
- HTML5地理位置地址表单填充器未捕获语法错误:意外的标记<
- Mongoose异步填充错误.未正确调用方法
- Chrome弧形路径填充错误的解决方法
- jquery mobile 1.3.1填充顶部在初始页面加载时计算错误
- 更改Img属性时出现填充数组错误
- 数组填充顺序错误
- Javascript数据表填充数据问题(没有返回错误)
- 猫鼬错误:在填充模型时没有为模型注册架构
- 猫鼬-自引用深度填充错误
- 谷歌地图地址自动填充错误:无法读取属性'长度'的定义
- 谷歌地图api kml填充错误