In this video, Emmanuel Henri completes the DELETE endpoint so you can start deleting specific contacts from your database. Then he tests the endpoint using Postman and Robot.
- [Instructor] The final endpoint we need to create is a delete API so we can delete any of the data we have in our database. So, let's get to it. So, let's go back to our controllers, so if you're just picking up this video from any other videos, you go inside of the controllers, crmController, and then just go at the end of that particular file, and we'll create a brand new function. So, we'll export it as well, and this one will be called deleteContact and we'll pass the request and response, like every other function in here, and then use the ContactCollection and the remove function from Mongoose.
There's also a findById and remove that I've seen some people use, but I always like to have less code in my application, so if I know that I can use another function, I will use it. So, let's use that function, and as parameters what it does, it takes an id, pretty much like the other function that we've created above, and it uses the request object, use the params, and it uses what is in the params, contactId, like so.
And then, what we'll do is, do the usual error in contact. Actually, we don't need the contact on this one, and do a function, and what we'll do is copy and paste this but we'll change the last one. So, let's do this first, so copy from line 37 through line 40, like so, and then paste it inside of our code at line 46, like so.
So, this is good, if there's an error, then respond with the error, and if there is, actually, we need to still pass the contact here, but if there is data, what I want you to do if the actual response is successful, so if we get a delete or an item deleted on their server, let's do something different here. So, what I want to do is do a message, so there's no way to actually state that the data has been deleted because it's no longer there, so what we want to do is create a message.
Once the response is successful, if the request is successful, actually, then we get the response back successfully deleted contact, like so. Okay, so I can see something's wrong, I see an error here, and if I look at my code, there shouldn't be any errors here. So, it says expected a comma, so that means something is wrong here. There you go, this is supposed to be a function, not an object.
So, we're good, let's save that. No errors on the server, we're all good there, so let's go ahead and go in Postman and grab, let's first of all grab all of our data. So, let's do a GET call with the contact URL, like so, and get out all of our data. So, what I'm going to do is, because I have two contacts here, let's delete this one here, so let's grab the id, like so, so copy that, and then put the id at the end of your URL here, and make sure that you do the DELETE call here.
So, instead of doing the GET, let's get the DELETE one. There's nothing to enter in the body because it's going to remove the entire, the entire id itself, well, the data here, so everything here should disappear from our database. And let's make sure we have Mongo Robo T open and ready to validate all this. So, let's go and close all this, and do a new search. So, we got two IDs right now in our contacts, those two, so let's go ahead and do the DELETE call.
So, send the request and the delete request is successful, and I see that we're missing something because it should've been successfully deleted, the contact, that means we haven't passed the function yet to our routes. So, let's go ahead and do that because the delete function is created, but we haven't passed it yet, so it's the old message here. So, let's go ahead and do the import of the deleteContact here. Let's scroll down to our delete here, so we remove everything from line 30 and the very last comma, all the way to the first one on line 29, like so, and then pass the deleteContact, save it again, no errors on the server.
Let's go back to Postman and let's do the DELETE request one more time, and now we got the message successfully deleted the contact. So, if do a GET request for that specific contact, we shouldn't see anything, it should actually send us an error. No, that's good. If we do a GET request for all the data, we should see the only one. And if we go back to our Mongo database, let's close that and then click on contacts again, we only have the one object.
Okay, so now we've got a full API with the proper endpoints to create, update, and delete items in the database.
- Setting up a project and a server
- Setting up a database and schema
- Creating POST, GET, PUT, and DELETE endpoints
- Serving files