populate in post hook middlewhere for 'find' in mongoose
That's because populate
is a method of the query object, not the document. You should use a pre
hook instead, like so:
ArticleSchema.pre('find', function () { // `this` is an instance of mongoose.Query this.populate('author');});
The above answers may not work because they are terminating the pre hook middleware by not calling next. The right implementation should be
productSchema.pre('find', function (next) {this.populate('category','name');this.populate('cableType','name');this.populate('color','names');next();
});
To add, the doc here is will allow your to continue to the next middleware.You can also use the following and select only some spefic fields. For instance the user model has name, email, address, and location
but you only want to populate only the name and email
ArticleSchema.pre('find', function () { // `this` is an instance of mongoose.Query this.populate({path: 'author', select: '-location -address'});});