Finding a single team

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

{"_id":"5a662fbf728726072c6298fc","name":"Peru","ranking":11,"captain":"Paolo Guerreo","Trainer":"Ricardo Gareca","confederation":"CONMEBOL","__v":0}

Cool! It is working. Note that we are using an existing ID—5a662fbf728726072c6298fc. This value will be different for you. To get a valid value, just call the /teams endpoint to list all your teams, and copy and replace it with the value in the _id attribute.

Now, what would happen if we pass an invalid ID? Let's test it:

$ curl http://localhost:3000/teams/5a662fbf728726072c629233

null

A null value is retrieved now. According to our API Documentation, we have to return the HTTP 404 status to represent a Not found response. So, to do this, we need to validate the result from the findById method and raise an error if we receive a null as a response. Go ahead and apply the following changes:

...
api
.route('/teams/:id')
.get((req, res, next) => {
let id = req.params.id
Team.findById(id)
.then(data => {
if (data === null) {
throw new Error("Team not found")
}

res.json(data)
})
.catch(err => { next(err) })
})
...

Now with this implementation, if we receive a null, we will raise an error that will be handled by our global error handler, which will send a JSON object with the error message. Let's test it:

$ curl http://localhost:3000/teams/5a662fbf728726072c629233

{"error":"Team not found"}

Lastly, we need to modify our error handler to change the HTTP status code to 404. In the server.js file, apply the following change:

...
app.use((err, req, res, next) => {
let status = 500

if (err.message.match(/not found/)) {
status = 404
}

return res.status(status).send({ error: err.message })
})
...

First, we declare the STATUS variable and assign 500 as its default value. Then, we apply a regular expression validation to check whether the message contains the not found string. If so, STATUS is changed to 404. So, let's test it again, adding the -v flag in the curl command to see the HTTP status:

curl http://localhost:3000/teams/5a662fbf728726072c629233 -v
...
>
< HTTP/1.1 404 Bad Request
...
{"error":"Team not found"}

That's it! Now with this, we are ready to learn how to update a team.

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

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