Welcome back.
In this video, let's go ahead and implement a route to fetch a single post.
So here are going to provide the idea of the post and then we can go ahead and fetch it for the meantime.
You only have one post here, but as you move on, I'm going to add more posts.
So now back to the server file.
We have a row to fetch a single post.
What is this?
So how can you find a single post?
Well, we are going to make use of JavaScript array method.
Remember we have the post array here and you're going to find the post by ID.
So inside this root here, let's remove this one and let's go ahead and then implement it step by step.
Step number one is get get the ID of the post first step.
How can we get it?
Remember upon calling the endpoint into pass in the ID?
So how can we get this ID inside the route and we can get it as rec params as I showed you in the previous
video.
So here id is going to be rec dot params dot id because this is where we specified here.
So now if I console.log the the ID here, let's save it now let me hit send here let me check the terminal
and indeed I have the ID here.
So now we are going to use this ID and find the red code inside the post array.
So now back here, the next step is we are going to find the post by ID.
So here we're going to be as find post by ID.
So here we go.
Const posts found.
Is equal to the post array the post here that we required.
And inside here we are going to make use of dot find.
So find return.
The first data that matches the search criteria or our criteria or our logic.
So here I'm going to make use of error function.
So for this function, it takes in one argument that is a callback function.
Then the post here.
So inside a callback function, I have access to the individual posts.
Now I can go ahead and return.
So here, make use, object and then return.
So here I'm going to check the post dot id if it is equal to the ID in the params.
So now let's go ahead and console.log.
The post found here and let's check in the console.
So let's remove this console.log or let's maintain it.
All right, so here, let's make the request.
When I hit send.
Now check the terminal.
Here we see that I get undefined because the ID here is 2020, but I pass in 23.
So let me provide here as 2020.
Let me hit send and check the terminal and indeed I have the posts.
So let's make our application more user friendly.
So let's throw an error if the post is not found.
So here we go.
We are going to make use of if there is no post found.
So if post is not found, then you go ahead and then send adjacent data with a message.
Says that post not found.
Let's provide ID which doesn't exist any ID, let's get signed and indeed I get a message post not found.
Now if the post is found, we can go ahead and then send to the user.
So here else.
Send the post to the user.
So here we go.
It's our red dot.
Just sayin.
And then the actual post.
Profound as that.
So now back to post man.
Let me hit send.
The post is not found.
Let's provide 2020 and let's see.
And I have my posts.
Let me save this request and let me search for all posts here.
Now look at difference for this one is an array because many posts and for a single post it's an object
because it's a single record.
The next video.
Let's see how we can add data onto post.
That is how we can make post requests.
Let's get into that in the next video.