Like API

The like API will be a PUT request to update the likes array in the Post document. The request will be received at the route api/posts/like.

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 received in the request body will be used to find the Post document and update it by pushing the current user's ID to the likes array.

mern-social/server/controllers/post.controller.js:

const like = (req, res) => {  Post.findByIdAndUpdate(req.body.postId, {$push: {likes: req.body.userId}}, {new: true})  .exec((err, result) => {    if (err) {      return res.status(400).json({        error: errorHandler.getErrorMessage(err)      })    }    res.json(result)  })

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.