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.exports
object in the routes/player.js file, define a newdeletePlayer
function withrequest
andresponse
parameters - In the body of the
deletePlayer
function, logrequest.params.id
to the console - Under the log, use
response.redirect
to redirect to the homepage:deletePlayer: function (request, response) { console.log(request.params.id); response.redirect('/'); }
- In the app.js file, under the
app.get
calls, add anotherapp.get
to hook up/delete/:id
toplayer.deletePlayer
- Run the app, navigate to the
/delete/5
route, and verify that5
is 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
for
looptd
with the "Edit" button - Within the
td
, add a newa
with the text "Delete" that points the user to/delete/
with the player'sid
- Set the
class
attribute of thea
to "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
deletePlayerById
to be anasync
function withplayerId
as a parameter - In the body of the
deletePlayerById
function, calldb.delete
onplayerId
- This will delete the key and value from the database
- Use the
await
keyword in front of thedb.delete
to 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
deletePlayer
function, remove theconsole.log
- In its place, create a new variable named
playerId
- Set
playerId
torequest.params.id
- Under that, add a call to
db.deletePlayerById
, passing inplayerId
- Put the
await
keyword in front ofdb.deletePlayerById
to wait for it to return - Add the
async
keyword in front of thedeletePlayer
function 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.