We need an endpoint to get user information by some identifier. To create the endpoint we need, follow these steps:
- Define the route:
routerAuth.get('/users/:id', User.show);
- Define the show method inside the user controller, server/api/user.js:
show: (req, res) => {
Model.users
.getBy(req.params.id)
.then(user => handleSuccess(res, { user: user.toJson() }))
.catch(errors => handleFailure(res, { errors, message: errors.message }));
},
- Define the getBy method inside the user model:
Schema.statics.getBy = function(id) {
return new Promise((resolve, reject) => {
try {
this.findOne({ _id: id })
.then(user => resolve(user))
.catch(error => reject(error));
} catch (e) {
reject(e);
}
});
};
As part of the exercise, we leave it up to you to test these endpoints.