I have a case where I am checking whether a document already exists and if it does not exists I am creating a new document. I need to populate 2 fields inside the document. My problem is that the .populate method is not supported on the .create method as I get an error if I try to do that. Furthermore, the .populate method is not working on the returned document as well. How do I correctly populate a newly created document ? Here is my code :
Favorite.create({ user: req.user._id, dishes: req.params.dishId })
.then((favorite) => {
favorite.populate('user');
favorite.populate('dishes');
console.log('Favorite marked', favorite);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(favorite);
}, (err) => next(err))
}
})
.catch((err) => next(err));
findById
first? – Pickerelweed