WEBVTT 0 00:00.270 --> 00:01.970 All right so here comes the answer. 1 00:03.490 --> 00:08.350 Now we know that we can tap into whatever value the user typed into here 2 00:08.410 --> 00:14.470 after that forward slash by tapping into req.params. the name of that parameter which is 3 00:14.470 --> 00:15.850 postName right? 4 00:16.060 --> 00:22.970 So let's go ahead and instead of console logging it, let's save that into a variable. 5 00:22.990 --> 00:30.600 Now let's say something like var requestedTitle is equal to req.params.postName. 6 00:30.640 --> 00:37.090 The next thing we need to do is we need to compare this requestedTitle against all the titles that 7 00:37.090 --> 00:40.690 we have inside our posts array 8 00:40.710 --> 00:41.200 right? 9 00:41.470 --> 00:48.370 So we know that posts is an array that contains a bunch of post objects. 10 00:48.370 --> 00:56.890 So each object has two key value pairs, a title and a content and the title inside this object is what 11 00:56.890 --> 00:58.350 we need to tap into. 12 00:58.450 --> 01:05.260 But because we only have access to this posts array in order to access each individual post we have 13 01:05.260 --> 01:07.520 to loop through the array. 14 01:07.960 --> 01:12.790 Fortunately we already know how to do that and how to do that quite easily. 15 01:12.790 --> 01:21.600 We can use our trusty forEach method in order to loop through each of the posts inside the post array. 16 01:21.610 --> 01:28.270 So if you remember the syntax was we specify the thing that we wanted to loop through which is our post 17 01:28.360 --> 01:30.460 array with an S at the end. 18 01:30.490 --> 01:31.810 This is the plural form. 19 01:32.110 --> 01:40.150 And then we call forEach on that array. And then we open up a set of parentheses and we put in an anonymous 20 01:40.150 --> 01:48.820 function. Inside the parameters of the function we specify the singular form of our array object. 21 01:48.820 --> 01:51.920 So that in this case is a single post 22 01:51.970 --> 01:58.720 right? Without the 's'. And now we open up a set of curly brackets and inside here we now have access to 23 01:58.780 --> 02:02.670 each and individual post inside our post array . 24 02:02.980 --> 02:10.780 That means that we can store this other variable which is called storedTitle and this is going to be 25 02:10.840 --> 02:15.310 equal to the post.title. 26 02:15.370 --> 02:23.530 So what our loop does currently is it will loop through all of the posts inside our post array 27 02:23.770 --> 02:30.630 and for each post, it's going to save it into a variable called storeTitle. 28 02:30.640 --> 02:36.670 Now some of your developer instincts should be tingling by now as we mentioned we should always start from 29 02:36.670 --> 02:40.710 const and then work upwards to let and var as needed. 30 02:40.720 --> 02:46.060 So let's go ahead and change that to const and change that to const. And you'll notice that we actually 31 02:46.060 --> 02:51.900 don't change the value of either of these inside the curly braces. 32 02:52.210 --> 02:55.100 These both work fine for our purposes. 33 02:55.570 --> 03:01.690 So now we have the requested title and each stored title Inside our array. 34 03:01.720 --> 03:05.460 Now it's time to check for each post 35 03:05.620 --> 03:10.910 whether if the storedTitle matches the requestedTitle so we can use an IF statement for that. 36 03:11.230 --> 03:19.780 And we check to see if storedTitle === the requestedTitle. 37 03:19.930 --> 03:25.350 Then in that case we're going to log match found. 38 03:26.020 --> 03:32.980 And now this is all the code that we need to loop through each of our posts, store each of the titles 39 03:33.070 --> 03:38.710 inside a constant called storeTitle and then we check if the storedTitle matches with the requested 40 03:38.710 --> 03:39.450 title 41 03:39.520 --> 03:42.040 and if so, we log match found. 42 03:42.040 --> 03:43.620 So let's go ahead and check it out. 43 03:44.700 --> 03:52.020 So once you resave our app.js you might notice that our home page is again devoid of any blog posts. 44 03:52.040 --> 03:55.410 Now in future modules, we're going to figure out how to fix this. 45 03:55.500 --> 04:01.020 But in order to do that we need to learn about databases and that's not the focus of this module. 46 04:01.020 --> 04:04.110 So let's just work with it as it is for now. 47 04:04.380 --> 04:11.820 And let's head over to compose and create a new post that again has the title of Test and then some 48 04:11.820 --> 04:14.110 Lorem Ipsum as the content. 49 04:14.130 --> 04:16.820 So now we know that we have this post called Test. 50 04:17.040 --> 04:21.920 And no matter how much we refresh, it still saved currently in our server. 51 04:22.500 --> 04:29.190 Let's go ahead to posts/Test and I'm going to spell it exactly the same way in terms 52 04:29.190 --> 04:36.660 of capitalization as the title of my post. And once I hit enter, you can see in your console that we get 53 04:36.660 --> 04:37.980 match found. 54 04:37.980 --> 04:40.910 If I go ahead and add another post... 55 04:46.480 --> 04:52.070 and I write Day 1, then you can see again we get match found. 56 04:52.270 --> 04:54.290 That's the solution to this challenge. 57 04:54.310 --> 04:59.920 If you've struggled with this challenge depending on which part you found difficult I recommend reviewing 58 04:59.920 --> 05:03.000 the lessons that covered those aspects. 59 05:03.010 --> 05:05.520 There's a couple of things that might have tripped you up around here. 60 05:05.710 --> 05:13.600 One is using the forEach to loop through our array which we covered previously in challenge 14. 61 05:13.830 --> 05:19.930 Or it might be that you got tripped up by the Express routing parameters which we covered just before 62 05:19.930 --> 05:22.120 challenge 16. 63 05:22.120 --> 05:27.870 So either pause the video and re-explore those topics that you find difficult or if this was a breeze, 64 05:27.940 --> 05:30.260 then continue on to the next challenge.