I have an article schema for articles posted on my site by users. It references the Users collection:
var ArticleSchema = new Schema({
title: { // NO MARKDOWN FOR THIS, just straight up text for separating from content
type: String,
required: true
},
author: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});
I want to have a post hook on all find/findOne calls to populate the reference:
ArticleSchema.post('find', function (doc) {
doc.populate('author');
});
For some reason, the doc that's returned in the hook does not have populate method. Do I have to populate using the ArticleSchema object instead of at the document level?
From MongooseJS Doc:
We cannot modify the result from inside the post find middleware as this refers to the query object.