1 00:00:01,380 --> 00:00:06,660 We've now defined the idea of a user inside of Mongoose so we can now start to use this user object 2 00:00:06,660 --> 00:00:11,870 right here to manipulate or query all the data that stored inside of our users collection. 3 00:00:11,890 --> 00:00:16,440 Now you'll notice that at no point time have we ever added in any additional configuration to Mongo 4 00:00:16,440 --> 00:00:20,130 DB to say hey there's this thing called the user's collection. 5 00:00:20,130 --> 00:00:24,720 Whenever we make use of Mongoose it's going to automatically take care of creating that collection for 6 00:00:24,720 --> 00:00:29,700 us and specifying the different properties that that collection is supposed to have. 7 00:00:29,700 --> 00:00:35,160 So by just defining the User model as we did in the last video mongoose is going to automatically create 8 00:00:35,160 --> 00:00:38,270 that collection for us and store all of our data inside there. 9 00:00:38,980 --> 00:00:43,710 So now we can use that user model that we just created to take the email and password out of the body 10 00:00:43,770 --> 00:00:45,720 of an incoming request. 11 00:00:45,720 --> 00:00:51,120 Back over here and use that to create a new user and stored inside of Mongo DB. 12 00:00:51,120 --> 00:00:56,400 Now remember I've said several times so we want to make sure that the incoming email and password whereas 13 00:00:56,400 --> 00:00:58,060 my diagram here is right here. 14 00:00:58,170 --> 00:01:02,430 We are definitely gonna want to make sure that that email is not already in use but will add in some 15 00:01:02,430 --> 00:01:04,130 code for that in just a moment. 16 00:01:04,150 --> 00:01:08,550 For right now let's just take the email and password that were provided to our request handler and use 17 00:01:08,550 --> 00:01:11,860 that to create a new user object okay. 18 00:01:11,890 --> 00:01:16,660 So back inside of my editor I'm going to find my post request handler. 19 00:01:16,830 --> 00:01:22,230 Remember this wrecked body property is an object that has an email and password property. 20 00:01:22,230 --> 00:01:27,290 So essentially just want take that email and password and use those to create our new user. 21 00:01:27,510 --> 00:01:30,960 So we'll add in something like const email and password. 22 00:01:30,960 --> 00:01:33,140 We'll do some D structuring here. 23 00:01:33,150 --> 00:01:38,160 We're going to take those two properties out of our rec body property and we're going to use them to 24 00:01:38,160 --> 00:01:39,320 create a new user. 25 00:01:39,400 --> 00:01:44,710 So to do so I'm going to say const user is going to be a new instance of a user. 26 00:01:45,060 --> 00:01:48,540 And we're going to pass in the properties that we want this new user to have. 27 00:01:48,570 --> 00:01:54,780 So I want them to have the email and password that came out of the request like so now by just creating 28 00:01:54,810 --> 00:01:59,990 a new instance of a user that doesn't actually save any data to our Mongo DB database. 29 00:02:00,030 --> 00:02:05,580 Instead we have to take this instance of a user that was just returned when we created it and call save 30 00:02:05,610 --> 00:02:12,640 on it we'll say user dot save like so anytime we call save that is an asynchronous operation because 31 00:02:12,640 --> 00:02:17,500 essentially Mongoose has to reach out over the Internet to that hosted instance of Mongo DV that we 32 00:02:17,500 --> 00:02:21,520 created just a little bit ago and initiate the save operation. 33 00:02:21,520 --> 00:02:27,430 So we're going to use the async await syntax to wait for that save operation to be completed to do so. 34 00:02:27,430 --> 00:02:36,340 I'm going to add in async to my request handler and I'll put in a wait right before user dot save OK. 35 00:02:36,370 --> 00:02:39,930 So that should be enough to actually get a new user created. 36 00:02:39,930 --> 00:02:46,050 So going to save this file and we'll go back over to post man and tried to create a new user so back 37 00:02:46,050 --> 00:02:47,040 inside of post man. 38 00:02:47,100 --> 00:02:52,220 I'm going to make sure that I've still got a body of raw with that given email and password. 39 00:02:52,270 --> 00:02:56,910 I'm gonna make sure that I still have a header of content type with application Jason. 40 00:02:57,540 --> 00:03:03,150 Once I've got that all in there or then click on send and that should it create my new user inside of 41 00:03:03,150 --> 00:03:04,750 Mongo DB. 42 00:03:04,830 --> 00:03:08,940 It looks like I did not get any error message because I'm still getting back the kind of you made a 43 00:03:08,940 --> 00:03:10,540 post request response right there. 44 00:03:10,710 --> 00:03:12,180 And I can also go back where my terminal. 45 00:03:12,180 --> 00:03:15,010 And just make sure I don't have any error over here as well. 46 00:03:15,050 --> 00:03:15,280 Okay. 47 00:03:15,360 --> 00:03:21,240 So I don't see any errors which means I must have created a new user successfully to verify that I can 48 00:03:21,240 --> 00:03:30,110 go back over to my Mongo DB database hosted at cloud dot Mongo dot me Mongo DB AECOM once here I can 49 00:03:30,110 --> 00:03:36,370 go back to that clusters tab and then click on the collections button once I click on a collections 50 00:03:36,430 --> 00:03:42,100 I'll now see that inside that test database that we made we've got a new collection of data called users 51 00:03:43,030 --> 00:03:49,630 and then inside there right now I've got exactly one object and that object has an I.D. tied to it and 52 00:03:49,630 --> 00:03:53,740 my email of test at test dot com and that password as well. 53 00:03:53,740 --> 00:03:58,930 So looks like we just successfully inserted a new user and we save them to our database which is definitely 54 00:03:58,930 --> 00:04:00,370 good progress. 55 00:04:00,370 --> 00:04:06,280 Now just one downside thing or one downside here you'll notice that we are storing our password in plaintext 56 00:04:06,430 --> 00:04:11,860 which means that if some malicious user ever got access to our database they could read out all of our 57 00:04:11,860 --> 00:04:17,320 passwords very easily which is definitely a huge thing that we want to never allow. 58 00:04:17,320 --> 00:04:22,770 We never want anybody to get access to plain text passwords inside of our database. 59 00:04:22,900 --> 00:04:26,650 So we'll take another quick pause right here when we come back the next video we're going to add in 60 00:04:26,710 --> 00:04:32,380 a little bit more code to our user dot J.S. file and make sure that we do not store our password in 61 00:04:32,380 --> 00:04:34,950 plain text anymore so click pause. 62 00:04:34,960 --> 00:04:36,400 We'll take care of that in the next video.