April 2020
Intermediate to advanced
716 pages
18h 55m
English
The unlike API will be implemented similar to the like API, with its own route. This will be declared as follows.
mern-social/server/routes/post.routes.js:
router.route('/api/posts/unlike') .put(authCtrl.requireSignin, postCtrl.unlike)
The unlike method in the controller will find the post by its ID and update the likes array by removing the current user's ID using $pull instead of $push. The unlike controller method will look as follows.
mern-social/server/controllers/post.controller.js:
const unlike = async (req, res) => { try { let result = await Post.findByIdAndUpdate(req.body.postId, {$pull: {likes: req.body.userId}}, {new: true}) res.json(result) } catch(err) { return res.status(400).json({ error: errorHandler.getErrorMessage(err) ...