mongodb-js / mongoose-autopopulate

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

autopopulate not working on findOneAndUpdate and findByIdAndUpdate. #42

Closed Mikou closed 6 years ago

Mikou commented 6 years ago

I am using mongoose-autopopulate on my model. It works fine when calling MyModel.findById(id). It fails however when calling MyModel.findByIdAndUpdate() or MyModel.findOneAndUpdate()

My calling code looks as follow: export const register = ({ params, user }, res, next) => { Event.findByIdAndUpdate( params.id, { $push: { registrations: { user } } }, { new: true } ) // I need to "manually" populate here //.populate('author') // .populate('registrations.user') .exec() .then(notFound(res)) .then(event => { console.log(event); return event ? event.view() : null; }) .then(success(res, 201)) .catch(next);

/Event.findById(params.id) .then(notFound(res)) .then(event => { event.registrations.push({ user }); return event.save(); }) .then(event => (event ? event.view() : null)) .then(success(res, 201)) .catch(next);/ };

vkarpov15 commented 6 years ago

Thanks for reporting, will fix ASAP

vkarpov15 commented 6 years ago

Fixed in 0.8.0 :+1: