1
00:00:03,469 --> 00:00:04,640
Welcome back.

2
00:00:04,670 --> 00:00:10,240
In this video, let's go ahead and implement a route to fetch a single post.

3
00:00:10,250 --> 00:00:17,360
So here are going to provide the idea of the post and then we can go ahead and fetch it for the meantime.

4
00:00:17,360 --> 00:00:21,680
You only have one post here, but as you move on, I'm going to add more posts.

5
00:00:21,680 --> 00:00:23,660
So now back to the server file.

6
00:00:23,660 --> 00:00:26,150
We have a row to fetch a single post.

7
00:00:26,150 --> 00:00:27,050
What is this?

8
00:00:27,050 --> 00:00:30,440
So how can you find a single post?

9
00:00:30,440 --> 00:00:35,000
Well, we are going to make use of JavaScript array method.

10
00:00:35,030 --> 00:00:41,570
Remember we have the post array here and you're going to find the post by ID.

11
00:00:41,600 --> 00:00:49,160
So inside this root here, let's remove this one and let's go ahead and then implement it step by step.

12
00:00:49,190 --> 00:00:57,950
Step number one is get get the ID of the post first step.

13
00:00:57,950 --> 00:00:59,510
How can we get it?

14
00:00:59,540 --> 00:01:04,069
Remember upon calling the endpoint into pass in the ID?

15
00:01:04,069 --> 00:01:12,650
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

16
00:01:12,650 --> 00:01:13,280
video.

17
00:01:13,280 --> 00:01:21,830
So here id is going to be rec dot params dot id because this is where we specified here.

18
00:01:22,040 --> 00:01:33,740
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

19
00:01:33,740 --> 00:01:36,740
and indeed I have the ID here.

20
00:01:36,800 --> 00:01:43,850
So now we are going to use this ID and find the red code inside the post array.

21
00:01:43,880 --> 00:01:50,390
So now back here, the next step is we are going to find the post by ID.

22
00:01:50,390 --> 00:01:57,650
So here we're going to be as find post by ID.

23
00:01:57,650 --> 00:01:58,730
So here we go.

24
00:01:58,730 --> 00:02:01,790
Const posts found.

25
00:02:02,610 --> 00:02:08,039
Is equal to the post array the post here that we required.

26
00:02:08,130 --> 00:02:13,260
And inside here we are going to make use of dot find.

27
00:02:13,560 --> 00:02:15,390
So find return.

28
00:02:15,390 --> 00:02:21,750
The first data that matches the search criteria or our criteria or our logic.

29
00:02:21,930 --> 00:02:24,210
So here I'm going to make use of error function.

30
00:02:24,210 --> 00:02:28,560
So for this function, it takes in one argument that is a callback function.

31
00:02:28,590 --> 00:02:30,090
Then the post here.

32
00:02:30,120 --> 00:02:34,350
So inside a callback function, I have access to the individual posts.

33
00:02:34,380 --> 00:02:36,750
Now I can go ahead and return.

34
00:02:36,930 --> 00:02:41,040
So here, make use, object and then return.

35
00:02:41,340 --> 00:02:49,020
So here I'm going to check the post dot id if it is equal to the ID in the params.

36
00:02:49,140 --> 00:02:52,230
So now let's go ahead and console.log.

37
00:02:52,230 --> 00:02:55,950
The post found here and let's check in the console.

38
00:02:55,980 --> 00:02:59,070
So let's remove this console.log or let's maintain it.

39
00:02:59,250 --> 00:03:01,920
All right, so here, let's make the request.

40
00:03:01,920 --> 00:03:03,440
When I hit send.

41
00:03:03,450 --> 00:03:04,910
Now check the terminal.

42
00:03:04,920 --> 00:03:13,020
Here we see that I get undefined because the ID here is 2020, but I pass in 23.

43
00:03:13,200 --> 00:03:17,220
So let me provide here as 2020.

44
00:03:17,250 --> 00:03:25,470
Let me hit send and check the terminal and indeed I have the posts.

45
00:03:25,620 --> 00:03:28,880
So let's make our application more user friendly.

46
00:03:28,890 --> 00:03:33,650
So let's throw an error if the post is not found.

47
00:03:33,660 --> 00:03:35,010
So here we go.

48
00:03:35,280 --> 00:03:40,450
We are going to make use of if there is no post found.

49
00:03:40,470 --> 00:03:49,440
So if post is not found, then you go ahead and then send adjacent data with a message.

50
00:03:50,160 --> 00:03:55,650
Says that post not found.

51
00:03:55,710 --> 00:04:04,350
Let's provide ID which doesn't exist any ID, let's get signed and indeed I get a message post not found.

52
00:04:04,380 --> 00:04:10,110
Now if the post is found, we can go ahead and then send to the user.

53
00:04:10,290 --> 00:04:12,630
So here else.

54
00:04:13,590 --> 00:04:19,370
Send the post to the user.

55
00:04:19,380 --> 00:04:20,370
So here we go.

56
00:04:20,399 --> 00:04:23,580
It's our red dot.

57
00:04:24,120 --> 00:04:25,170
Just sayin.

58
00:04:25,170 --> 00:04:27,780
And then the actual post.

59
00:04:28,750 --> 00:04:30,140
Profound as that.

60
00:04:30,160 --> 00:04:32,650
So now back to post man.

61
00:04:32,650 --> 00:04:34,090
Let me hit send.

62
00:04:34,090 --> 00:04:35,710
The post is not found.

63
00:04:35,770 --> 00:04:39,000
Let's provide 2020 and let's see.

64
00:04:39,010 --> 00:04:41,050
And I have my posts.

65
00:04:41,110 --> 00:04:44,710
Let me save this request and let me search for all posts here.

66
00:04:44,740 --> 00:04:52,780
Now look at difference for this one is an array because many posts and for a single post it's an object

67
00:04:52,780 --> 00:04:54,760
because it's a single record.

68
00:04:54,850 --> 00:04:56,130
The next video.

69
00:04:56,140 --> 00:04:59,980
Let's see how we can add data onto post.

70
00:05:00,070 --> 00:05:02,550
That is how we can make post requests.

71
00:05:02,560 --> 00:05:04,810
Let's get into that in the next video.

