1 00:00:00,210 --> 00:00:06,960 In this video as promised it'll be up to you to define a new amount use model and actually use it down 2 00:00:06,960 --> 00:00:07,340 below. 3 00:00:07,350 --> 00:00:14,340 What I'm gonna do is comment out our existing call to save and the new model we create as we don't want 4 00:00:14,340 --> 00:00:20,850 to add another user when we run the challenge code and down below I have the challenge comments outlining 5 00:00:20,850 --> 00:00:22,450 what I'd like you to do. 6 00:00:22,470 --> 00:00:29,040 Your goal is to create a new model for tasks you're going to define the model with a description and 7 00:00:29,040 --> 00:00:35,940 completed fields exactly like we had before and then you're going to create a new instance of the model 8 00:00:36,000 --> 00:00:37,610 and save it to the database. 9 00:00:37,620 --> 00:00:39,940 Last up you'll be testing your work. 10 00:00:39,960 --> 00:00:47,010 This is similar to what we did up above when we defined the User model created a new instance of it 11 00:00:47,220 --> 00:00:51,270 and then saved it to the database using the save method. 12 00:00:51,270 --> 00:00:58,140 Now for the task we want description to be a string and we want completed to be a boolean. 13 00:00:58,140 --> 00:01:05,700 You can use boolean types by simply using uppercase B boolean like we've done with string and number 14 00:01:05,730 --> 00:01:06,660 before. 15 00:01:06,780 --> 00:01:09,430 All right take some time to knock this one out. 16 00:01:09,540 --> 00:01:16,650 Test your work by actually viewing that new task over inside of the database once you're done come back 17 00:01:16,830 --> 00:01:21,600 and click play. 18 00:01:21,610 --> 00:01:22,720 How did you do. 19 00:01:22,780 --> 00:01:26,300 Let's go ahead and kick things off by defining the model. 20 00:01:26,350 --> 00:01:35,050 So right here a const uppercase t task and then we'll be using a mongoose dot model to define the second 21 00:01:35,050 --> 00:01:36,580 model we're working with. 22 00:01:36,580 --> 00:01:43,390 Remember we passed in a name for the model task and we also passed in an object and this is where we 23 00:01:43,390 --> 00:01:46,450 define what exactly makes up a task. 24 00:01:46,600 --> 00:01:53,730 In our case we are choosing description and completed as the two fields so we can start with description. 25 00:01:53,770 --> 00:01:58,860 We'll set this equal to an object and then we'll set it's type equal to a string. 26 00:01:58,880 --> 00:02:05,200 Sends the description will indeed be a string and the only other thing we're gonna set up is completed 27 00:02:07,010 --> 00:02:09,020 and completed will also have a type. 28 00:02:09,020 --> 00:02:13,000 And as mentioned I wanted you to set that equal to ebullient. 29 00:02:13,070 --> 00:02:17,740 From here we can move on to Step Two and create a new instance of the model. 30 00:02:17,780 --> 00:02:25,400 So down below I'm going to create a new task that's lower case t making sure it doesn't conflict with 31 00:02:25,400 --> 00:02:30,820 the upper case T variable up above and we'll use our constructor function. 32 00:02:30,920 --> 00:02:38,180 We're going to create a new task providing the object and this is where we define the data for the task 33 00:02:38,240 --> 00:02:39,140 we're creating. 34 00:02:39,140 --> 00:02:46,910 I'll go ahead and set description equal to and I'll just use a generic description something like learn 35 00:02:47,480 --> 00:02:55,070 the Mongoose library and then we'll go ahead and set completed for the task and I haven't learned everything 36 00:02:55,070 --> 00:02:56,330 about it just yet. 37 00:02:56,330 --> 00:02:59,570 So I'll leave that as false incomplete. 38 00:02:59,570 --> 00:03:05,900 Now that we have the task instance created that's step two we can actually use the same method to make 39 00:03:05,900 --> 00:03:08,210 sure things get saved to the database. 40 00:03:08,210 --> 00:03:12,130 So down below task dot save is what we're gonna use. 41 00:03:12,200 --> 00:03:18,740 Now we mentioned earlier that save returns a promise so we can add our promise method calls right on 42 00:03:18,740 --> 00:03:19,490 there. 43 00:03:19,490 --> 00:03:25,730 I'm gonna add then before when things go well and then down below I'll also set up a catch to handle 44 00:03:25,730 --> 00:03:33,770 those potential errors and if things do go well console dialog I'll just dump the task to the terminal 45 00:03:34,010 --> 00:03:40,930 and if things go wrong I'll just dump the error right here console dialog printing the error to the 46 00:03:40,930 --> 00:03:42,070 terminal. 47 00:03:42,070 --> 00:03:47,050 Now that's all of the code we need to write that is steps 1 2 and 3. 48 00:03:47,050 --> 00:03:49,810 Let's go ahead and run the file to test our work. 49 00:03:49,870 --> 00:03:51,390 It should define the task. 50 00:03:51,400 --> 00:03:56,140 Create a new instance of it and save that new instance to the database. 51 00:03:56,140 --> 00:03:58,600 I'm going to rerun the script and what do we get. 52 00:03:58,600 --> 00:04:05,500 I can see that while I do get a notification about something completely unrelated I also have my object 53 00:04:05,500 --> 00:04:07,270 printing down below. 54 00:04:07,270 --> 00:04:11,300 Normally I put on do not disturb mode on my Mac when I record. 55 00:04:11,410 --> 00:04:13,570 I must've forgot but I just enabled it. 56 00:04:14,140 --> 00:04:19,510 So this object contains all of the properties we were expecting and the last thing worth doing is to 57 00:04:19,510 --> 00:04:23,570 just make sure we're also seeing this data in robo 3 T. 58 00:04:23,680 --> 00:04:25,870 So that would be under a new collection. 59 00:04:26,020 --> 00:04:33,220 I can right click collections and click refresh to see my tasks then I can go ahead and click View documents 60 00:04:33,400 --> 00:04:36,160 to view the one task we've created. 61 00:04:36,280 --> 00:04:39,970 And right here we have the exact values we provided. 62 00:04:39,970 --> 00:04:48,190 Now you'll notice that we have lower case you users and lower case t tasks but at no point did we ever 63 00:04:48,190 --> 00:04:56,230 write that Mongoose actually takes the model name you provide task right here and up above user and 64 00:04:56,230 --> 00:04:59,440 it converts it to lower case and it paralyzes it. 65 00:04:59,510 --> 00:05:08,530 It then uses that as the collection name so user gets stored in users and a task gets stored in tasks. 66 00:05:08,530 --> 00:05:14,920 We'll see some of the other things that Mongoose does for us as we continue to explore its features. 67 00:05:14,920 --> 00:05:18,190 That's exactly what we're gonna do in the next video. 68 00:05:18,190 --> 00:05:20,650 So I'm gonna remove those challenge comments. 69 00:05:20,740 --> 00:05:26,140 I'll save the file for the final time in this one and I'll see you in the next lesson.