簡體   English   中英

如何注冊和使用 mongoose-deep-populate

[英]How to register and use mongoose-deep-populate

好的。 所以我正在嘗試對我的mongoose模型進行一些嵌套填充。 我發現Mongoose-Deep-Populate是嵌套解決方案最簡單的修復方法之一,但是,盡管看起來很簡單,但我不知道如何正確注冊插件。

我正在使用一個angular-fullstack項目,並且剛剛添加了deep-populate插件的npm install 然后我進入我的 model 並添加了這個:

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

正如您所看到的,在我的 model 中可以有多層嵌套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);
            };
        });
};

然而,這是行不通的。 而且我很確定我做錯了什么。 我已經在 .net 上查過了,但似乎找不到可以澄清我的錯誤的例子。 我現在來找你們我偉大的大師們尋求解決我的麻煩的方法。

試試這個代碼

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

如果你遇到任何困難。 請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM