Getting a single user information

We need an endpoint to get user information by some identifier. To create the endpoint we need, follow these steps:

  1. Define the route:
routerAuth.get('/users/:id', User.show);
  1. 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 }));
},
  1. 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. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset