INNER CODE UNIT · JavaScript
deleteComment
ihtasham42/social-media-app · controllers/commentControllers.js:128
const deleteComment = async (req, res) => {
try {
const commentId = req.params.id;
const { userId, isAdmin } = req.body;
const comment = await Comment.findById(commentId);
if (!comment) {
throw new Error("Comment not found");
}
if (comment.commenter != userId && !isAdmin) {
throw new Error("Not authorized to delete comment");
}
await comment.remove();
const post = await Post.findById(comment.post);