April 2020
Intermediate to advanced
716 pages
18h 55m
English
The like API will be a PUT request that will update the likes array in the Post document. The request will be received at the api/posts/like route, which is defined as follows.
mern-social/server/routes/post.routes.js:
router.route('/api/posts/like') .put(authCtrl.requireSignin, postCtrl.like)
In the like controller method, the post ID that's received in the request body will be used to find the specific Post document and update it by pushing the current user's ID to the likes array, as shown in the following code.
mern-social/server/controllers/post.controller.js:
const like = async (req, res) => { try { let result = await Post.findByIdAndUpdate(req.body.postId, {$push: {likes: req.body.userId}}, {new: true}) res.json(result) ...Read now
Unlock full access