Hi I'm new to mongodb and node.js. I have the following abbreviated schema:
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
likes: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'user'
}
}
],...
I want to access each Post by id (passed through request params) and then remove a like based on the user id. Presently, it removes the like from the array but does not then throw any errors when I try again to remove the same id from the db. Here is my code at the moment:
const like = await Post.findByIdAndUpdate(
req.params.id,
{ $pull: { likes: { user: req.user.id } } },
(error, result) => {
if (!error) {
return res.json('Post unliked');
}
return res.status(400).send('You have not liked this post');
}
);