好的。所以我正在尝试对我的mongoose
模型进行一些嵌套填充。我发现Mongoose-Deep-Populate
是嵌套解决方案中最简单的修复程序之一,然而,尽管看起来很简单,但我不知道如何正确注册插件。
我使用的是一个angular-fullstack
项目,刚刚添加了deep-populate
插件的npm install
。然后我进入我的模型,添加了这个:
'use strict';
var deepPopulate = require('mongoose-deep-populate');
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var EntrySchema = new Schema({
articleId: Number,
title: String,
content: String,
date: Date,
children: [{
type: Schema.ObjectId,
ref: 'Entry'
}],
forum: {
type: Schema.ObjectId,
ref: 'Forum'
},
language: {
type: Schema.ObjectId,
ref: 'Language'
},
orphan: Boolean,
writer: {
type: Schema.ObjectId,
ref: 'User'
},
type: String
}).plugin(deepPopulate, options);
module.exports = mongoose.model('Entry', EntrySchema);
正如您所看到的,在我的模型中可以有多层嵌套的entries
。为了找到它们,我写了这样的文章:
// Get list of chapter entries
exports.chapter = function(req, res) {
Entry.find()
.where('orphan').equals(true)
.populate('children')
.exec(function (err, entrys) {
Entry.deepPopulate(docs, 'children', err, entrys) {
if(err) {
return handleError(res, err);
}
return res.json(200, entrys);
};
});
};
然而,这不起作用。我确信我做错了什么。我在网上查了一下,但似乎找不到一个能澄清我错误的例子。我现在来找你们,我的大师们,寻求解决我的麻烦的办法。
试试这个代码
exports.chapter = function(req, res) {
Entry.find()
.where('orphan').equals(true)
.populate('children')
.exec(function (err, entrys) {
mongoose.deepPopulate(entrys, 'children', err, function(err, entrys) {
if(err) {
return handleError(res, err);
}
return res.json(200, entrys);
});
});
};
如果你遇到任何困难。请告诉我