mongodb-js / mongoose-autopopulate

Always populate() certain fields in your mongoose schemas
Apache License 2.0
222 stars 36 forks source link

populate is not working! #97

Open toufik2015 opened 2 years ago

toufik2015 commented 2 years ago

The pre hook is not working! and it doesn't populate the teacher field.

`const mongoose = require('mongoose');

const assignmentSchema = new mongoose.Schema( { teacher: { type: mongoose.Schema.ObjectId, ref: 'User' }, group: { type: mongoose.Schema.ObjectId, ref: 'Group' }, AssignedAt: { type: Date, default: Date.now() }, }, { toJSON: { virtuals: true }, toObject: { virtuals: true } } );

const Assignment = mongoose.model('Assignment', assignmentSchema);

assignmentSchema.pre(/^find/, function (next) { console.log('is this running?'); this.populate({ path: 'teacher', select: 'firstName', }); next(); });

module.exports = Assignment;`

vkarpov15 commented 2 years ago

@toufik2015 what version of Mongoose?

Also, why are you reporting this issue here? It doesn't look like you're using mongoose-autopopulate