How to register and use mongoose

in order. so I'm trying to make some nested population of my mongoose models. And I found that Mongoose-Deep-Populate was one of the easiest fixes for the embedded solution, however, no matter how easy it may seem, I can’t figure out how to register the plugin correctly.

I am using the angular-fullstack and just added the npm install the deep-populate . And then I went into my model and added:

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

As you can see, in my model you can have several levels of nested entries . Therefore, to find them all, I wrote this:

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

However, this does not work. And I'm quite sure that I did something wrong. I checked the network, but did not seem to be able to find an example that could clarify my mistake. And now I have come to you, my great masters, to solve my problems.

+5
source share

Source: https://habr.com/ru/post/1215693/


All Articles