In this video, Emmanuel Henri completes the GET endpoint with an ID so you can start pulling specific contacts from your database. Then he tests the endpoint using Postman and Robot.
- [Instructor] What if you need to retrieve just the information of a specific user. This is when a specific ID endpoint comes into play. Lets get to it. So the first thing I want you to do, again, is go inside of the controllers. And we'll create a new controller. So let's go ahead and do export, constant. And this one we'll do get contact with ID.
And same as before, request, response. And this is a function so we'll pass these two items and we'll find, so this is a function that comes with Mongoose and Mongo. Find by idea. And you see, because I have auto complete and most of the commands that come with the Mongo are in here. I can do find by ID, just like so.
And in the request object we'll be able to pass the params and the contact ID. Once we make our request, we'll be able to pass the contact ID here inside of our request. So once we get into our controller, in the request and the params we'll be able to grab that contact ID and therefore find data based off that ID. And then we'll do the same as we've done before.
So, if there's an error, do this. If there's data, do that. And we can literally copy what we have in here for this one. And paste it in there. Because it's the same thing. If there's an error then do the response and send the error. If there's no errors then send the data. So this is pretty much how it works when you want to find someone by a specific ID or find data by specific ID.
You loan the collection, find by ID pass whatever is the ID, in this case we'll get it from the request that we're doing. And then do the response. Whatever is the case. If there's an error then response would respond with the error. If there's no errors then respond with the contact. That's a semi colon here. And make sure we have a line of dm. Save that. Now, all we have to do is pass that function inside of our get request but for that specific ID.
So, again, we had, for that URL those request. Now we have for this URL here those request that we can make. So we'll add a brand new one. Which will be the get one. So lets go ahead and do that. And I'll put a note here so you know what this is. So get specific ID or specific contact. And then lets create that function. So, to a get and simply pass the get ID, get contact with ID.
And make sure we have it loaded in this specific area here too. So lets go higher here and make sure we have it imported from the controllers. Get contact with ID and let me make sure that I have the same spelling here because that could be an issue. So what I just did is copy and I'll paste it here. Like so. So, lets save that and lets just add a space here.
This is a put request and lets finish our code and make sure that we have the comments here for a delete request. Like so. Yeah, so that looks a little bit cleaner. We have the right function pass to our get request. And it will be passed to that specific URL. So lets save this, make sure our servers running properly. And lets test this in Postman.
So, now that we have to IDs here in our database. Lets grab the ID of this one here. So copy the ID of the first one or copy the ID of the second one. It doesn't really matter. So copy that. Not cut but copy. And then paste it here at the end of your URL. So at local hose 3000 forward slash contact pass the specific ID that you want to test inside of your get request.
And by doing this and doing a get request you should be able to send it and get the data from that specific user. So now if you need any specific contact and their data you can pull only a specific contact by its ID. So we're almost there with completing our endpoints. Lets move on to building the rest.
- Setting up a project and a server
- Setting up a database and schema
- Creating POST, GET, PUT, and DELETE endpoints
- Serving files