April 2020
Intermediate to advanced
716 pages
18h 55m
English
We will implement an uncomment API at the following PUT route.
mern-social/server/routes/post.routes.js:
router.route('/api/posts/uncomment') .put(authCtrl.requireSignin, postCtrl.uncomment)
The uncomment controller method will find the relevant post by ID and pull the comment with the deleted comment's ID from the comments array in the post, as implemented in the following code.
mern-social/server/controllers/post.controller.js:
const uncomment = async (req, res) => { let comment = req.body.comment try{ let result = await Post.findByIdAndUpdate(req.body.postId, {$pull: {comments: {_id: comment._id}}}, {new: true}) .populate('comments.postedBy', '_id name') .populate('postedBy', '_id name') .exec() res.json(result) } catch(err) ...Read now
Unlock full access