All right.
So here's the answer to the challenge.
Now if you had any trouble trying to figure out how to create an object in Javascript then you would've
come across the syntax for an object that looks something like this.
So you would start with the keyword var to show that you're creating a new variable, then you would give
that variable a name whatever it is that you wish,
then you have an equal sign and you open a set of curly braces.
Now inside the curly braces you have your key value pairs and you can have as many pairs as you wish
and you can even nest key value pairs as the value for a particular key.
So in this case the key and values that are in red is actually bunched together as a value for the second
key.
So with this refresher on Javascript objects in mind let's head over to our app.js and let's delete
this console that log. And instead of console logging our post body and post title we're going to store
it inside a object.
Let's use the same syntax as we saw previously in the slides.
So that's create a var and we'll call it a post and we're going to use the equal sign and then we to open
up a set of curly braces.
And this is the start of any Javascript object. Inside the curly braces
I'm going to put in my key value pairs.
So the two keys that I want are the title and the content.
Now all I have to do is just to add the values of each of those keys. So we know that the content is
basically this
right?
It's request.
body.postBody.
And- So the title is structured very similarly request.body.postTitle and then we have a comma
separating our key value pairs and we complete object by closing it off with a semicolon.
So now we can delete our console.log and we now have our brand new object that's called post that contains
two key value pairs separated by a comma,
one is going to be the title and one is going to be the content. And these values are passed over
when that post route is triggered.
Now the final thing to consider is that we mentioned previously that you should try to avoid using vars
if at all possible.
And the best type of programming practice is to start with the most restrictive type of variable which
is a const.
And as you remember consts cannot be changed after they've been created. And we're not planning on
changing it inside here after it's been declared. We're only interested in storing it and then we'll
use it inside one of our pages.
So a const is probably the best keyword to put over here. Now if you use the var or if you use the let,
that's completely fine. But just take a refresher of what we talked about in the scope lesson and
when we mentioned these three types of variables.
So now we're ready to move on to the next challenge.