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