April 2020
Intermediate to advanced
716 pages
18h 55m
English
When a single user is retrieved from the backend, we want the user object to include the names and IDs of the users referenced in the following and followers arrays. To retrieve these details, we need to update the userByID controller method so that it populates the returned user object, as shown in the highlighted code.
mern-social/server/controllers/user.controller.js:
const userByID = async (req, res, next, id) => { try { let user = await User.findById(id) .populate('following', '_id name') .populate('followers', '_id name') .exec() if (!user) return res.status('400').json({ error: "User not found" }) req.profile = user next() } catch (err) { return res.status('400').json({ error: "Could not retrieve ...Read now
Unlock full access