Hey, did everyone are they sure?
And welcome to another video.
In the previous video, we saw that how we can use the sign up.
And I did a classic mistake up here, the comma instead of the dot.
So not the dot, it should be a comma.
And again, the good thing is I don't have to just check this mistake into five different files since
I have broken down the cookie token.
Luckily for me, I can just change it and hopefully this should work.
So this is one typo that we have done in this video we're going to work through and we'll connect our
database.
So that's going to be basics of it.
So let's go ahead and do that.
Now for the database connection, I would like to have the database URL as well.
So let's go ahead and connect this one.
So I'll call this one as simply DB, underscore URL.
Feel free to name these variables, whatever you like, it doesn't really matter.
Nothing at all.
So let's go ahead and call this one.
So this one is going to be MongoDB Colon, slash, slash, and then we need to go ahead and 1 to 7 .0.0.1
and then we have to grab a port as well, which is 27017 and what should I call our DB?
Let's go ahead and call this one as ae d shirt store because I might be having a couple of other DBAs
as well.
So I think this long I didn't I don't have it.
So let's go ahead and grab this one up here.
So this is the basic again, in case your URL of the DB, you are using the atlas or something, just
go ahead and inject that URL as simple as it sounds.
Once I have this one, all it remains is to connect the database and to see that where I'm going to
inject that functionality.
We have done that once in the past as well.
Let's go into the config and create a new file.
This file will be simply DB JS because that's exactly what it does.
It just connects with the database itself.
In order to connect with the database, we are going to need a mongoose.
So let's go ahead and require that.
Require there we go.
And we're going to say mongoose.
The mongoose has a simple functionality of connecting with the database, but we won't be holding it
like that.
We will be holding that into a method itself, and we'll be throwing this method out from this config
and will be importing that into index or JS.
So let's go ahead and say that and we're going to call this one as Connect with DB.
And there we go, nice and easy, just like that.
Now obviously this method needs to be thrown out.
So we're going to go ahead and say module dot x sports and let's go ahead and say connect with DB.
Now, inside this one, all we got to do is connect with the database itself.
So let's go ahead and do that.
Now, Mongoose can actually connect with the database by simply throwing a property of Dot Connect.
Now here it is.
It will be definitely be able to connect there, but there might be things that can go wrong or things
that can go right.
So obviously after the connection, you can actually go ahead and chain on a dot then and a dot catch
as well.
Okay, so this is all nice.
Now let's go ahead and try to connect with that.
So for connection, we need a string.
So we're going to go ahead and say process dot, envy dot, whatever we called it into.
E and V files db url.
So let's copy that.
And there we go.
Now, once this is all connected, hopefully then we are going to go ahead and simply provide some of
the properties that we did in the past as well.
So let's go ahead and do that.
Not the user they use new URL parser, so use new URL parser.
So there we go.
And pass on this flag as true.
Hopefully I didn't made any typos up here.
Otherwise database will definitely tell me up here.
And we need to use the unified topology as well.
So we're going to say use unified.
Now it doesn't suggest me their unified topology.
There we go.
And this needs to be turned on as true as well.
There we go.
Nice and easy.
Okay, so it looks like this is all good and OC what happens if everything gets connected successfully?
We go ahead and simply log this message that simply DB was connected.
DB DB got connected.
And again, let's go ahead and remove this part and what happens in the cache part in case you are not
able to connect it properly.
Let's go ahead.
And in this case first, let's receive the error.
And once the error is being received, then we're going to provide a couple of console logs, console
log, the first one got a DB come on and DB connection issues.
And after that we're going to just go ahead and log the errors, whatever the errors we got.
Just like that.
And we need to exit the process as well.
So we're going to go ahead and remove this one.
Yep.
There we go.
And after that, let's go ahead and say process dot exit gracefully.
There we go.
So this is it.
In case I haven't made any type of which we are going to get see in a minute.
So this is all what we need up here.
Okay.
Looks great.
Looks fantastic.
Now we need to go up here and in the index.
You can actually go ahead and start your database as soon as your app starts.
But I think in my case, I would I would actually prefer that to get started as soon as the index file
actually runs.
So let's go ahead and first bring this up.
So let's go ahead and say I want to bring in connect.
I can actually go ahead and try auto import, maybe connect with database.
Yep.
There we go.
So it has auto imported this line up for me and before the server actually starts, I would like to
go ahead and connect this one just like that.
Would love to add a comment here that says Connect with database.
Okay, so now this should be all good.
All I need to do is I don't even need to restart it says DB got connected and server is running so hopefully
things are all good and all fine.
Now further down the road, we need to now check that whatever the method that we have created is all
working fine or not.
But I think breaking it down into a separate video actually makes sense.
So let's go ahead and move on to the next video and check our sign up route.