7

好的。所以我正在尝试对我的mongoose模型进行一些嵌套填充。而且我发现这Mongoose-Deep-Populate是嵌套解决方案最简单的修复之一,但是,尽管看起来很简单,但我无法弄清楚如何正确注册插件。

我正在使用一个angular-fullstack项目,只是添加npm installdeep-populate插件。然后我进入我的模型并添加了这个:

'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);
            };
        });
};

但是,这不起作用。而且我很确定我做错了什么。我在网上查了一下,但似乎无法找到一个可以澄清我的错误的例子。现在我来找你们,我的大师们,为我解决我的烦恼。

4

0 回答 0