WEBVTT

00:03.760 --> 00:05.110
Hey, did everyone are they sure?

00:05.110 --> 00:06.640
And welcome to another video.

00:06.670 --> 00:09.550
In the previous video, we saw that how we can use the sign up.

00:09.550 --> 00:13.420
And I did a classic mistake up here, the comma instead of the dot.

00:13.420 --> 00:15.790
So not the dot, it should be a comma.

00:15.790 --> 00:20.980
And again, the good thing is I don't have to just check this mistake into five different files since

00:20.980 --> 00:22.540
I have broken down the cookie token.

00:22.540 --> 00:25.570
Luckily for me, I can just change it and hopefully this should work.

00:25.570 --> 00:30.130
So this is one typo that we have done in this video we're going to work through and we'll connect our

00:30.130 --> 00:30.700
database.

00:30.700 --> 00:32.290
So that's going to be basics of it.

00:32.290 --> 00:33.700
So let's go ahead and do that.

00:33.760 --> 00:38.110
Now for the database connection, I would like to have the database URL as well.

00:38.140 --> 00:40.060
So let's go ahead and connect this one.

00:40.060 --> 00:42.790
So I'll call this one as simply DB, underscore URL.

00:42.790 --> 00:46.540
Feel free to name these variables, whatever you like, it doesn't really matter.

00:46.780 --> 00:47.440
Nothing at all.

00:47.440 --> 00:48.910
So let's go ahead and call this one.

00:48.910 --> 00:57.700
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

00:58.000 --> 01:05.620
and then we have to grab a port as well, which is 27017 and what should I call our DB?

01:05.620 --> 01:13.960
Let's go ahead and call this one as ae d shirt store because I might be having a couple of other DBAs

01:13.960 --> 01:14.230
as well.

01:14.230 --> 01:16.540
So I think this long I didn't I don't have it.

01:16.540 --> 01:18.340
So let's go ahead and grab this one up here.

01:18.340 --> 01:23.620
So this is the basic again, in case your URL of the DB, you are using the atlas or something, just

01:23.620 --> 01:26.380
go ahead and inject that URL as simple as it sounds.

01:27.240 --> 01:32.220
Once I have this one, all it remains is to connect the database and to see that where I'm going to

01:32.220 --> 01:33.300
inject that functionality.

01:33.300 --> 01:34.920
We have done that once in the past as well.

01:34.920 --> 01:37.410
Let's go into the config and create a new file.

01:37.410 --> 01:40.860
This file will be simply DB JS because that's exactly what it does.

01:40.860 --> 01:42.780
It just connects with the database itself.

01:42.900 --> 01:45.780
In order to connect with the database, we are going to need a mongoose.

01:45.780 --> 01:47.640
So let's go ahead and require that.

01:48.090 --> 01:49.950
Require there we go.

01:49.950 --> 01:51.810
And we're going to say mongoose.

01:52.230 --> 01:56.400
The mongoose has a simple functionality of connecting with the database, but we won't be holding it

01:56.400 --> 01:56.910
like that.

01:56.910 --> 02:02.490
We will be holding that into a method itself, and we'll be throwing this method out from this config

02:02.490 --> 02:04.890
and will be importing that into index or JS.

02:04.890 --> 02:11.820
So let's go ahead and say that and we're going to call this one as Connect with DB.

02:12.660 --> 02:15.990
And there we go, nice and easy, just like that.

02:17.640 --> 02:19.680
Now obviously this method needs to be thrown out.

02:19.680 --> 02:25.140
So we're going to go ahead and say module dot x sports and let's go ahead and say connect with DB.

02:25.350 --> 02:29.190
Now, inside this one, all we got to do is connect with the database itself.

02:29.190 --> 02:30.600
So let's go ahead and do that.

02:30.720 --> 02:35.610
Now, Mongoose can actually connect with the database by simply throwing a property of Dot Connect.

02:35.610 --> 02:36.420
Now here it is.

02:36.420 --> 02:41.400
It will be definitely be able to connect there, but there might be things that can go wrong or things

02:41.400 --> 02:42.270
that can go right.

02:42.270 --> 02:48.120
So obviously after the connection, you can actually go ahead and chain on a dot then and a dot catch

02:48.120 --> 02:48.810
as well.

02:50.220 --> 02:52.110
Okay, so this is all nice.

02:52.110 --> 02:53.970
Now let's go ahead and try to connect with that.

02:53.970 --> 02:55.380
So for connection, we need a string.

02:55.380 --> 03:00.540
So we're going to go ahead and say process dot, envy dot, whatever we called it into.

03:02.000 --> 03:04.040
E and V files db url.

03:04.040 --> 03:05.300
So let's copy that.

03:05.510 --> 03:06.530
And there we go.

03:07.280 --> 03:12.350
Now, once this is all connected, hopefully then we are going to go ahead and simply provide some of

03:12.350 --> 03:14.750
the properties that we did in the past as well.

03:14.750 --> 03:16.100
So let's go ahead and do that.

03:16.310 --> 03:23.960
Not the user they use new URL parser, so use new URL parser.

03:25.070 --> 03:26.030
So there we go.

03:26.030 --> 03:27.800
And pass on this flag as true.

03:27.830 --> 03:30.080
Hopefully I didn't made any typos up here.

03:30.170 --> 03:32.450
Otherwise database will definitely tell me up here.

03:32.450 --> 03:34.790
And we need to use the unified topology as well.

03:34.790 --> 03:38.150
So we're going to say use unified.

03:38.150 --> 03:43.400
Now it doesn't suggest me their unified topology.

03:43.430 --> 03:44.210
There we go.

03:44.450 --> 03:46.910
And this needs to be turned on as true as well.

03:46.910 --> 03:47.470
There we go.

03:47.480 --> 03:48.500
Nice and easy.

03:49.440 --> 03:54.680
Okay, so it looks like this is all good and OC what happens if everything gets connected successfully?

03:54.680 --> 03:58.880
We go ahead and simply log this message that simply DB was connected.

03:59.240 --> 04:04.070
DB DB got connected.

04:04.790 --> 04:10.100
And again, let's go ahead and remove this part and what happens in the cache part in case you are not

04:10.100 --> 04:11.660
able to connect it properly.

04:11.660 --> 04:12.290
Let's go ahead.

04:12.290 --> 04:15.740
And in this case first, let's receive the error.

04:15.740 --> 04:21.380
And once the error is being received, then we're going to provide a couple of console logs, console

04:21.380 --> 04:30.980
log, the first one got a DB come on and DB connection issues.

04:31.430 --> 04:36.230
And after that we're going to just go ahead and log the errors, whatever the errors we got.

04:38.030 --> 04:39.470
Just like that.

04:39.800 --> 04:41.840
And we need to exit the process as well.

04:41.840 --> 04:44.420
So we're going to go ahead and remove this one.

04:45.440 --> 04:45.800
Yep.

04:45.800 --> 04:46.520
There we go.

04:46.790 --> 04:53.040
And after that, let's go ahead and say process dot exit gracefully.

04:53.060 --> 04:53.750
There we go.

04:54.110 --> 04:55.370
So this is it.

04:55.370 --> 04:58.520
In case I haven't made any type of which we are going to get see in a minute.

04:58.520 --> 05:00.800
So this is all what we need up here.

05:01.250 --> 05:01.520
Okay.

05:01.550 --> 05:02.080
Looks great.

05:02.090 --> 05:03.050
Looks fantastic.

05:03.080 --> 05:05.570
Now we need to go up here and in the index.

05:05.570 --> 05:09.590
You can actually go ahead and start your database as soon as your app starts.

05:09.590 --> 05:15.350
But I think in my case, I would I would actually prefer that to get started as soon as the index file

05:15.350 --> 05:15.860
actually runs.

05:15.860 --> 05:17.570
So let's go ahead and first bring this up.

05:17.570 --> 05:21.380
So let's go ahead and say I want to bring in connect.

05:21.380 --> 05:28.430
I can actually go ahead and try auto import, maybe connect with database.

05:28.430 --> 05:28.730
Yep.

05:28.730 --> 05:29.210
There we go.

05:29.210 --> 05:34.190
So it has auto imported this line up for me and before the server actually starts, I would like to

05:34.190 --> 05:36.610
go ahead and connect this one just like that.

05:36.620 --> 05:42.720
Would love to add a comment here that says Connect with database.

05:43.710 --> 05:46.430
Okay, so now this should be all good.

05:46.430 --> 05:52.280
All I need to do is I don't even need to restart it says DB got connected and server is running so hopefully

05:52.280 --> 05:54.170
things are all good and all fine.

05:54.290 --> 05:58.850
Now further down the road, we need to now check that whatever the method that we have created is all

05:58.850 --> 06:00.020
working fine or not.

06:00.020 --> 06:03.530
But I think breaking it down into a separate video actually makes sense.

06:03.530 --> 06:06.650
So let's go ahead and move on to the next video and check our sign up route.
