We've now defined the idea of a user inside of Mongoose so we can now start to use this user object
right here to manipulate or query all the data that stored inside of our users collection.
Now you'll notice that at no point time have we ever added in any additional configuration to Mongo
DB to say hey there's this thing called the user's collection.
Whenever we make use of Mongoose it's going to automatically take care of creating that collection for
us and specifying the different properties that that collection is supposed to have.
So by just defining the User model as we did in the last video mongoose is going to automatically create
that collection for us and store all of our data inside there.
So now we can use that user model that we just created to take the email and password out of the body
of an incoming request.
Back over here and use that to create a new user and stored inside of Mongo DB.
Now remember I've said several times so we want to make sure that the incoming email and password whereas
my diagram here is right here.
We are definitely gonna want to make sure that that email is not already in use but will add in some
code for that in just a moment.
For right now let's just take the email and password that were provided to our request handler and use
that to create a new user object okay.
So back inside of my editor I'm going to find my post request handler.
Remember this wrecked body property is an object that has an email and password property.
So essentially just want take that email and password and use those to create our new user.
So we'll add in something like const email and password.
We'll do some D structuring here.
We're going to take those two properties out of our rec body property and we're going to use them to
create a new user.
So to do so I'm going to say const user is going to be a new instance of a user.
And we're going to pass in the properties that we want this new user to have.
So I want them to have the email and password that came out of the request like so now by just creating
a new instance of a user that doesn't actually save any data to our Mongo DB database.
Instead we have to take this instance of a user that was just returned when we created it and call save
on it we'll say user dot save like so anytime we call save that is an asynchronous operation because
essentially Mongoose has to reach out over the Internet to that hosted instance of Mongo DV that we
created just a little bit ago and initiate the save operation.
So we're going to use the async await syntax to wait for that save operation to be completed to do so.
I'm going to add in async to my request handler and I'll put in a wait right before user dot save OK.
So that should be enough to actually get a new user created.
So going to save this file and we'll go back over to post man and tried to create a new user so back
inside of post man.
I'm going to make sure that I've still got a body of raw with that given email and password.
I'm gonna make sure that I still have a header of content type with application Jason.
Once I've got that all in there or then click on send and that should it create my new user inside of
Mongo DB.
It looks like I did not get any error message because I'm still getting back the kind of you made a
post request response right there.
And I can also go back where my terminal.
And just make sure I don't have any error over here as well.
Okay.
So I don't see any errors which means I must have created a new user successfully to verify that I can
go back over to my Mongo DB database hosted at cloud dot Mongo dot me Mongo DB AECOM once here I can
go back to that clusters tab and then click on the collections button once I click on a collections
I'll now see that inside that test database that we made we've got a new collection of data called users
and then inside there right now I've got exactly one object and that object has an I.D. tied to it and
my email of test at test dot com and that password as well.
So looks like we just successfully inserted a new user and we save them to our database which is definitely
good progress.
Now just one downside thing or one downside here you'll notice that we are storing our password in plaintext
which means that if some malicious user ever got access to our database they could read out all of our
passwords very easily which is definitely a huge thing that we want to never allow.
We never want anybody to get access to plain text passwords inside of our database.
So we'll take another quick pause right here when we come back the next video we're going to add in
a little bit more code to our user dot J.S. file and make sure that we do not store our password in
plain text anymore so click pause.
We'll take care of that in the next video.