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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?