June 2018
Intermediate to advanced
348 pages
8h 19m
English
The delete process is similar to the update. First, we need to retrieve a valid index from the teams array and then remove it from there. Open the teams-api.js file and apply the following changes:
...api .route('/teams/:id') .get((req, res) => { ... }) .put((req, res) => { ... }) .delete((req, res) => { const id = req.params.id const index = lookupTeam(id) const team = teams[index] if (index !== -1) { teams.splice(index, 1) return res.send(team) } return res.status(404).send('team not found') })...
So we define a .delete route and look up for a team by passing the id param. If a valid index is returned, we save the team instance in the team variable. Next, we delete the element from the array using the splice(index, 1) expression. ...
Read now
Unlock full access