Skip to Content
Full-Stack React Projects - Second Edition
book

Full-Stack React Projects - Second Edition

by Shama Hoque
April 2020
Intermediate to advanced
716 pages
18h 55m
English
Packt Publishing
Content preview from Full-Stack React Projects - Second Edition

The Unlike API

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Full-Stack React Projects

Full-Stack React Projects

Shama Hoque
React Native - The Practical Guide

React Native - The Practical Guide

Academind by Maximilian Schwarzmüller GmbH

Publisher Resources

ISBN: 9781839215414Supplemental Content