In this video as promised it'll be up to you to define a new amount use model and actually use it down
below.
What I'm gonna do is comment out our existing call to save and the new model we create as we don't want
to add another user when we run the challenge code and down below I have the challenge comments outlining
what I'd like you to do.
Your goal is to create a new model for tasks you're going to define the model with a description and
completed fields exactly like we had before and then you're going to create a new instance of the model
and save it to the database.
Last up you'll be testing your work.
This is similar to what we did up above when we defined the User model created a new instance of it
and then saved it to the database using the save method.
Now for the task we want description to be a string and we want completed to be a boolean.
You can use boolean types by simply using uppercase B boolean like we've done with string and number
before.
All right take some time to knock this one out.
Test your work by actually viewing that new task over inside of the database once you're done come back
and click play.
How did you do.
Let's go ahead and kick things off by defining the model.
So right here a const uppercase t task and then we'll be using a mongoose dot model to define the second
model we're working with.
Remember we passed in a name for the model task and we also passed in an object and this is where we
define what exactly makes up a task.
In our case we are choosing description and completed as the two fields so we can start with description.
We'll set this equal to an object and then we'll set it's type equal to a string.
Sends the description will indeed be a string and the only other thing we're gonna set up is completed
and completed will also have a type.
And as mentioned I wanted you to set that equal to ebullient.
From here we can move on to Step Two and create a new instance of the model.
So down below I'm going to create a new task that's lower case t making sure it doesn't conflict with
the upper case T variable up above and we'll use our constructor function.
We're going to create a new task providing the object and this is where we define the data for the task
we're creating.
I'll go ahead and set description equal to and I'll just use a generic description something like learn
the Mongoose library and then we'll go ahead and set completed for the task and I haven't learned everything
about it just yet.
So I'll leave that as false incomplete.
Now that we have the task instance created that's step two we can actually use the same method to make
sure things get saved to the database.
So down below task dot save is what we're gonna use.
Now we mentioned earlier that save returns a promise so we can add our promise method calls right on
there.
I'm gonna add then before when things go well and then down below I'll also set up a catch to handle
those potential errors and if things do go well console dialog I'll just dump the task to the terminal
and if things go wrong I'll just dump the error right here console dialog printing the error to the
terminal.
Now that's all of the code we need to write that is steps 1 2 and 3.
Let's go ahead and run the file to test our work.
It should define the task.
Create a new instance of it and save that new instance to the database.
I'm going to rerun the script and what do we get.
I can see that while I do get a notification about something completely unrelated I also have my object
printing down below.
Normally I put on do not disturb mode on my Mac when I record.
I must've forgot but I just enabled it.
So this object contains all of the properties we were expecting and the last thing worth doing is to
just make sure we're also seeing this data in robo 3 T.
So that would be under a new collection.
I can right click collections and click refresh to see my tasks then I can go ahead and click View documents
to view the one task we've created.
And right here we have the exact values we provided.
Now you'll notice that we have lower case you users and lower case t tasks but at no point did we ever
write that Mongoose actually takes the model name you provide task right here and up above user and
it converts it to lower case and it paralyzes it.
It then uses that as the collection name so user gets stored in users and a task gets stored in tasks.
We'll see some of the other things that Mongoose does for us as we continue to explore its features.
That's exactly what we're gonna do in the next video.
So I'm gonna remove those challenge comments.
I'll save the file for the final time in this one and I'll see you in the next lesson.