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

Fetching user posts in the view

We will use the list-posts-by-user API in the frontend to fetch the related posts and display these posts in the profile view. To use this API, we will add a fetch method to the frontend, as follows.

mern-social/client/post/api-post.js:

const listByUser = async (req, res) => {  try {    let posts = await Post.find({postedBy: req.profile._id})                          .populate('comments.postedBy', '_id name')                          .populate('postedBy', '_id name')                          .sort('-created')                          .exec()    res.json(posts)  } catch(err) {    return res.status(400).json({      error: errorHandler.getErrorMessage(err)    })  }}

This fetch method will load the required posts for PostList, which is added to the Profile view. We will update the Profile component so that it defines a loadPosts ...

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