API for posts by a user

The route that will receive a query to return posts by a specific user will be added in mern-social/server/routes/post.routes.js:

router.route('/api/posts/by/:userId')    .get(authCtrl.requireSignin, postCtrl.listByUser)

The listByUser controller method in post.controller.js will query the Post collection to find posts that have a matching reference in the postedBy field to the user specified in the userId param in the route.

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

const listByUser = (req, res) => {  Post.find({postedBy: req.profile._id})  .populate('comments', 'text created')  .populate('comments.postedBy', '_id name')  .populate('postedBy', '_id name')  .sort('-created')  .exec((err, posts) => {    if (err) { return ...

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.