June 2018
Intermediate to advanced
348 pages
8h 19m
English
To find a single team, we will make use of the findById built-in method, and we will pass a valid ID to it. So, apply the following changes:
api .route('/teams/:id') .get((req, res, next) => { let id = req.params.id Team.findById(id) .then(data => res.json(data)) .catch(err => next(err)) }) .put((req, res) => { // TODO }) .delete((req, res) => { // TODO })
First, we extract the ID from the req.params object. Note that we are not using the /teams route. Instead, we are using the /teams/:id route. It means that Express.js will inject the id attribute as an element of the params object. Then, we call the findById method and send the response to the client. Let's test it:
$ curl http://localhost:3000/teams/5a662fbf728726072c6298fc ...
Read now
Unlock full access