All right so here comes the answer. 1
Now we know that we can tap into whatever value the user typed into here 2
after that forward slash by tapping into req.params. the name of that parameter which is 3
postName right? 4
So let's go ahead and instead of console logging it, let's save that into a variable. 5
Now let's say something like var requestedTitle is equal to req.params.postName. 6
The next thing we need to do is we need to compare this requestedTitle against all the titles that 7
we have inside our posts array 8
right? 9
So we know that posts is an array that contains a bunch of post objects. 10
So each object has two key value pairs, a title and a content and the title inside this object is what 11
we need to tap into. 12
But because we only have access to this posts array in order to access each individual post we have 13
to loop through the array. 14
Fortunately we already know how to do that and how to do that quite easily. 15
We can use our trusty forEach method in order to loop through each of the posts inside the post array. 16
So if you remember the syntax was we specify the thing that we wanted to loop through which is our post 17
array with an S at the end. 18
This is the plural form. 19
And then we call forEach on that array. And then we open up a set of parentheses and we put in an anonymous 20
function. Inside the parameters of the function we specify the singular form of our array object. 21
So that in this case is a single post 22
right? Without the 's'. And now we open up a set of curly brackets and inside here we now have access to 23
each and individual post inside our post array . 24
That means that we can store this other variable which is called storedTitle and this is going to be 25
equal to the post.title. 26
So what our loop does currently is it will loop through all of the posts inside our post array 27
and for each post, it's going to save it into a variable called storeTitle. 28
Now some of your developer instincts should be tingling by now as we mentioned we should always start from 29
const and then work upwards to let and var as needed. 30
So let's go ahead and change that to const and change that to const. And you'll notice that we actually 31
don't change the value of either of these inside the curly braces. 32
These both work fine for our purposes. 33
So now we have the requested title and each stored title Inside our array. 34
Now it's time to check for each post 35
whether if the storedTitle matches the requestedTitle so we can use an IF statement for that. 36
And we check to see if storedTitle === the requestedTitle. 37
Then in that case we're going to log match found. 38
And now this is all the code that we need to loop through each of our posts, store each of the titles 39
inside a constant called storeTitle and then we check if the storedTitle matches with the requested 40
title 41
and if so, we log match found. 42
So let's go ahead and check it out. 43
So once you resave our app.js you might notice that our home page is again devoid of any blog posts. 44
Now in future modules, we're going to figure out how to fix this. 45
But in order to do that we need to learn about databases and that's not the focus of this module. 46
So let's just work with it as it is for now. 47
And let's head over to compose and create a new post that again has the title of Test and then some 48
Lorem Ipsum as the content. 49
So now we know that we have this post called Test. 50
And no matter how much we refresh, it still saved currently in our server. 51
Let's go ahead to posts/Test and I'm going to spell it exactly the same way in terms 52
of capitalization as the title of my post. And once I hit enter, you can see in your console that we get 53
match found. 54
If I go ahead and add another post... 55
and I write Day 1, then you can see again we get match found. 56
That's the solution to this challenge. 57
If you've struggled with this challenge depending on which part you found difficult I recommend reviewing 58
the lessons that covered those aspects. 59
There's a couple of things that might have tripped you up around here. 60
One is using the forEach to loop through our array which we covered previously in challenge 14. 61
Or it might be that you got tripped up by the Express routing parameters which we covered just before 62
challenge 16. 63
So either pause the video and re-explore those topics that you find difficult or if this was a breeze, 64
then continue on to the next challenge.