I have an empty array in MongoDB
And I want to push the Id in it.
router.put(
"/like/:id",
(req, res) => {
User.findOne({ _id: req.user._id }).then(data => {
if (data) {
Post.update(
{ _id: ObjectId(req.params.id) },
{ $push: { likes: req.user._id } }
);
}
});
}
);
This code is not working on how I achieve this.
findOne()in Mongoose returns aQuery, not a Promise. You need to chain theexecmethod for you to get a promise i.e.User.findOne({ _id: req.user._id }).exec().then(...)orUser.findById(req.user._id).exec().then(...)