When working in this function, I search for the specific post
using the postId
. Then, within that post, I locate a particular comment
based on the commentId
. After that, I verify if the array of nested objects likes
contains the userId
. If not found, I add the userId
to the likes
array. However, despite following these steps, the userId
is still not being added to the likes
array. What could be causing this issue?
const likeComment = async (req, res) => {
try {
const post = await Post.findById(req.body.postId);
const comment = post.comments.find(
(comment) => comment.id === req.body.commentId
);
if (comment.likes.includes(req.body.userId)) {
var index = comment.likes.indexOf(req.body.userId);
if (index !== -1) {
comment.likes.splice(index, 1);
}
res.status(200).json("you disliked this comment");
} else {
comment.likes.push(req.body.userId);
res.status(200).json("you liked this comment");
}
} catch (err) {
console.log(err);
res.status(500).json(err);
}
};