Delete a Quote2:56 with Treasure Porth
In this video, we'll add functionality to our app that will allow the client to delete quotes.
The delete route is going to be a lot like the update quote route, 0:00 only it will completely remove the specified quote from the data store. 0:04 To create a delete route, 0:08 we'll want to handle delete requests sent to the slash quote slash ID route. 0:09 Again, we'll need the callback to be an asynchronous function that takes in 0:18 the request and response objects. 0:21 And to use the async await syntax, 0:28 we'll need a try-catch block.bbb Inside the catch block, 0:31 we'll copy and paste the same error handling code from previous videos. 0:36 Inside the try block, we'll again get the specified quote by grabbing the ID from 0:46 the request parameters and passing it to the get quote function. 0:50 Once we’ve got the quote we’re looking for, 1:03 we can use the delete quote method from our records module. 1:05 We'll wanna wait for the delete quote method to finish before we send back 1:13 the proper status message and end the request. 1:17 We'll send back the same no content status message. 1:26 Let's save this and take a peek inside of our data store. 1:30 From testing and such, I have several duplicate quotes to delete. 1:34 Go to postman and send a delete request to the ID of the quote you want to delete. 1:38 So I want to delete quote 1492. 1:45 Send. 1:51 And close this for now. 1:53 Let's see if we can figure out what went wrong here. 1:56 I can see I have a typo in my route. 1:59 So let's try this again. 2:03 And we've got a 204 No Content status code. 2:05 Great, let's take a look inside data.json. 2:08 And that quote is gone. 2:13 For practice, I encourage you to look at the put route we did in a previous 2:15 video,and implement some extra additional error handling for our delete route. 2:19 What if the client requests to delete a quote that doesn't exist? 2:25 Well done. 2:29 We now have a fully functioning rest API with all of its cred operations. 2:30 We can create, read, update, and delete quotes. 2:34 You may have noticed that we have quite a bit of unwieldy and repeating code. 2:37 In the next part of the course, we'll use express middleware to clean up 2:41 some of our repeating code, see ways to structure our projects so 2:45 that our code is more modular, and learn how we can handle errors globally. 2:48 And of course, we'll write a route that returns a random quote. 2:52 I'll see you there. 2:55
You need to sign up for Treehouse in order to download course files.Sign up