Deleting a Comment (Authorized)

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 329:- Deleting a Comment (Authorized)

To delete a comment, we first need to create a route for handling the deletion of comments. In the routes folder, create a new file called commentRoutes.js and add the following code:

javascriptCopy code

const express = require('express'); const router = express.Router({ mergeParams: true }); const { ensureAuthenticated } = require('../config/auth'); const Post = require('../models/Post'); const Comment = require('../models/Comment'); // Delete a comment router.delete('/:commentId', ensureAuthenticated, async (req, res) => {  try {    const { commentId } = req.params;    const { postId } = req.params;    // Find the comment by id and remove it    const deletedComment = await Comment.findByIdAndRemove(commentId);    // Find the post that the comment belongs to and remove the comment id from its comments array    const post = await Post.findByIdAndUpdate(postId, {      $pull: { comments: commentId },    });    res.status(200).json({ message: 'Comment deleted successfully' });  } catch (error) {    console.error(error);    res.status(500).json({ message: 'Server Error' });  } }); module.exports = router;

In this file, we first import the necessary modules and models. Then, we define a DELETE route for deleting a comment. We use the ensureAuthenticated middleware to ensure that the user is logged in before allowing them to delete a comment.

In the route handler, we extract the commentId and postId from the request parameters. We use findByIdAndRemove to find and remove the comment by its id. We then use findByIdAndUpdate to find the post that the comment belongs to and remove the comment id from its comments array using $pull. Finally, we send a JSON response indicating that the comment was deleted successfully.

Now we need to add this route to our app. In the app.js file, add the following code:

phpCopy code

const commentRoutes = require('./routes/commentRoutes'); app.use('/posts/:postId/comments', commentRoutes);

This tells our app to use the commentRoutes for URLs that match the pattern /posts/:postId/comments/*, where * can be any string.

With this in place, we can now delete comments by sending a DELETE request to /posts/:postId/comments/:commentId, where :postId is the id of the post that the comment belongs to and :commentId is the id of the comment to be deleted.

38. Deleting And Updating Objects in Database + Distributing Views

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support