1
00:00:00,320 --> 00:00:05,510
Okay, so now we want to be able to connect to our database through our application so that we can make

2
00:00:05,510 --> 00:00:06,110
queries.

3
00:00:06,110 --> 00:00:11,210
And we're going to use a tool called Mongoose to do that, which is an object data mapper.

4
00:00:11,380 --> 00:00:11,640
Okay.

5
00:00:11,690 --> 00:00:12,800
So we can connect to it.

6
00:00:12,800 --> 00:00:17,870
As you can see here, we bring it in, we call Connect, we put in our string and then from there we

7
00:00:17,870 --> 00:00:19,760
can create what are called models.

8
00:00:19,790 --> 00:00:20,010
Okay?

9
00:00:20,030 --> 00:00:24,830
We create a schema, which is basically the fields that you want for each model.

10
00:00:24,830 --> 00:00:30,260
So for example, we're going to have a products model and we'll have a schema that'll have fields like

11
00:00:30,260 --> 00:00:34,190
the product name, the price, the description and so on.

12
00:00:34,220 --> 00:00:34,460
Okay.

13
00:00:34,460 --> 00:00:39,680
Basically everything that we have in that in the JavaScript file that we're currently fetching from.

14
00:00:39,680 --> 00:00:42,890
So we want to install this in our back end dependencies.

15
00:00:42,890 --> 00:00:47,960
So and you can read more about the documentation if you want, but we're going to go into the route.

16
00:00:47,960 --> 00:00:54,170
I'm just going to stop the server, make sure you're in the route and let's say NPM install mongoose.

17
00:00:57,260 --> 00:01:03,380
Now what we want to do is create a config file in the back end to connect to our database.

18
00:01:03,380 --> 00:01:07,070
So I'm going to create a folder in back end called config.

19
00:01:07,070 --> 00:01:14,090
And then inside config let's create a file called DB dot JS and then this is where we want to connect

20
00:01:14,090 --> 00:01:15,710
to our database from.

21
00:01:15,710 --> 00:01:22,220
So we're going to import Mongoose and then we're going to have a function called connect.

22
00:01:23,030 --> 00:01:25,880
DB and we're going to make it a synchronous.

23
00:01:25,880 --> 00:01:33,620
I'm going to try not to use copilot so much here, but it's going to be a synchronous because any any

24
00:01:34,400 --> 00:01:40,040
methods that we call, whether it's from a mongoose model or from mongoose itself, it's going to return

25
00:01:40,040 --> 00:01:40,640
a promise.

26
00:01:40,640 --> 00:01:46,700
So you can either do the dot catch syntax or you can do a async await, which is what we're doing.

27
00:01:46,700 --> 00:01:50,570
So we don't need to pass in any arguments or anything but in the body.

28
00:01:50,570 --> 00:01:52,430
Let's do a try catch.

29
00:01:53,720 --> 00:02:02,690
And in the try we're going to create a variable called con and set that to await Mongoose dot connect.

30
00:02:02,720 --> 00:02:06,680
Now, we don't need a second object here or anything.

31
00:02:07,930 --> 00:02:08,039
Yeah.

32
00:02:08,120 --> 00:02:09,110
So just like that.

33
00:02:09,110 --> 00:02:12,830
Now what's happening is we're calling Mongoose Connect, which returns a promise.

34
00:02:12,830 --> 00:02:18,530
So we're awaiting on it and then we're accessing the Mongo Uri from our environment variable.

35
00:02:19,070 --> 00:02:19,330
All right.

36
00:02:19,340 --> 00:02:24,950
And then after that, we'll just do a console log and let's put in some backticks.

37
00:02:24,950 --> 00:02:30,260
We'll say MongoDB connected and we'll put the host as well.

38
00:02:30,920 --> 00:02:31,250
All right.

39
00:02:31,250 --> 00:02:34,820
So if everything goes okay and we're connected, we should see this.

40
00:02:34,850 --> 00:02:38,600
If there's an error, then we're just going to do a console dot.

41
00:02:38,630 --> 00:02:45,590
We'll do console dot log and excuse me, let's pass in some backticks and we'll say error and then we

42
00:02:45,590 --> 00:02:47,300
can just get the error message.

43
00:02:47,300 --> 00:02:50,480
And then I want to exit the process with error.

44
00:02:50,480 --> 00:03:00,240
So we're going to say process dot exit and pass in a one and then we just need to export default connect

45
00:03:00,240 --> 00:03:00,630
DB.

46
00:03:01,870 --> 00:03:06,490
So basically we just need to call this at the beginning of our application to connect.

47
00:03:06,490 --> 00:03:11,140
So let's go back into Server.js and we need to bring in the file.

48
00:03:11,140 --> 00:03:12,640
So I'm going to do that.

49
00:03:13,390 --> 00:03:15,940
Um, I'll just do that right here.

50
00:03:15,940 --> 00:03:19,060
So let's say import.

51
00:03:19,940 --> 00:03:20,600
Connect.

52
00:03:20,600 --> 00:03:26,150
DB And that's going to be from config db, JS and then we just need to call that.

53
00:03:26,150 --> 00:03:29,600
So I'm going to put it, see, I'm going to call it.

54
00:03:31,490 --> 00:03:31,940
Let's go.

55
00:03:31,940 --> 00:03:35,740
Right, right, right above where we initialize the app.

56
00:03:35,750 --> 00:03:37,700
So we're just going to say Connect DB.

57
00:03:41,220 --> 00:03:42,720
All right, So let's save that.

58
00:03:42,720 --> 00:03:45,150
And now I'm just going to run the server to test it out.

59
00:03:45,150 --> 00:03:48,120
So NPM run server.

60
00:03:49,670 --> 00:03:50,420
And there we go.

61
00:03:50,420 --> 00:03:53,750
So we get MongoDB connected and it gives us the host.

62
00:03:53,780 --> 00:03:57,440
Now, if you see something else, if you see an error, make sure you read it.

63
00:03:57,440 --> 00:04:00,920
And it's probably going to have to do with your connection string.

64
00:04:00,920 --> 00:04:06,890
So this right here, maybe your password or username is wrong or the name of your database, or if you

65
00:04:06,890 --> 00:04:13,790
go into the Atlas dashboard, you might, you might not have added your IP address correctly because

66
00:04:13,790 --> 00:04:18,019
only the IP addresses that you add there can connect to the database.

67
00:04:18,170 --> 00:04:18,890
All right.

68
00:04:18,890 --> 00:04:23,990
But you can also select for anywhere, you know, anywhere be able to connect as well.

69
00:04:24,440 --> 00:04:26,900
So just, just double check that stuff.

70
00:04:27,110 --> 00:04:32,750
So now that we're connected in the next video, what we're going to do is start to model our data because

71
00:04:32,750 --> 00:04:38,450
before we can actually fetch and display the products from the database or import them or whatever we're

72
00:04:38,450 --> 00:04:40,610
going to do, we need to model our data.

73
00:04:40,610 --> 00:04:41,780
So we'll do that next.

