April 2020
Intermediate to advanced
716 pages
18h 55m
English
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 ...