CRUD App - Delete
The last step for the CRUD app is allowing the user to delete objects from the database. Add this ability with a GET request that includes a player's id as a route parameter.
Routing the GET Handler
NOTE: It is not best practice to use an HTTP GET request to perform a DELETE operation. However, using a GET handler in this instance works fine, so it is used for the sake of simplicity.
- In the module.exportsobject in the routes/player.js file, define a newdeletePlayerfunction withrequestandresponseparameters
- In the body of the deletePlayerfunction, logrequest.params.idto the console
- Under the log, use response.redirectto redirect to the homepage:deletePlayer: function (request, response) { console.log(request.params.id); response.redirect('/'); }
- In the app.js file, under the app.getcalls, add anotherapp.getto hook up/delete/:idtoplayer.deletePlayer
- Run the app, navigate to the /delete/5route, and verify that5is properly logged in the console as theid!
Adding the Delete Buttons
Now that the "Delete Player" route exists, the user needs a way to navigate to it! Add "Delete" buttons for each player in the home page table.
- In the views/index.ejs file, find the forlooptdwith the "Edit" button
- Within the td, add a newawith the text "Delete" that points the user to/delete/with the player'sid
- Set the classattribute of theato "btn btn-sm btn-danger" to make it appear like a red button:<a href="/delete/<%= players[i].id %>" class="btn btn-sm btn-danger">Delete</a>
- Load up the homepage, and verify that the "Delete" button links to the "Delete" route for the proper player!
At this point, the player ID should simply be logged in the console. The program will end up using that ID to delete the player data.
Deleting the Player from the DB
The GET handler function should delete the player with the given id from the database.
DB Function
Start by creating a function that can delete a player from the database based on its ID.
- In the db.js file, add a new module export property named deletePlayerById
- Set the value of deletePlayerByIdto be anasyncfunction withplayerIdas a parameter
- In the body of the deletePlayerByIdfunction, calldb.deleteonplayerId- This will delete the key and value from the database
 
- Use the awaitkeyword in front of thedb.deleteto properly wait for the result
Code
deletePlayerById: async function(playerId) {
    await db.delete(playerId);
}
Route Function
Next, update the /delete/:id GET handler to use the DB function to delete the data in the DB. Open the routes/player.js file to begin.
- In the deletePlayerfunction, remove theconsole.log
- In its place, create a new variable named playerId
- Set playerIdtorequest.params.id
- Under that, add a call to db.deletePlayerById, passing inplayerId
- Put the awaitkeyword in front ofdb.deletePlayerByIdto wait for it to return
- Add the asynckeyword in front of thedeletePlayerfunction because it uses anawait
Code
deletePlayer: async function (request, response) {
    let playerId = request.params.id;
    await db.deletePlayerById(playerId);
    response.redirect('/');
}
At this point, it should be possible to load up the homepage and delete a player! Click the "Delete" button next to one of the players, and verify that that player disappears. Try deleting all players, and verify that the "No players found." message appears.
Now all four of the CRUD operations should be working! Congratulations, you've built a CRUD app.