1
00:00:00,690 --> 00:00:06,960
We have successfully connected to the database and life is great, but before we start messing around

2
00:00:06,960 --> 00:00:14,760
with the data, there's something that's bugging and simply the fact that our server and database connection

3
00:00:15,060 --> 00:00:16,710
don't work in sync.

4
00:00:17,070 --> 00:00:17,950
Now, what do I mean?

5
00:00:17,970 --> 00:00:19,850
Well, let's take a look at the console.

6
00:00:20,280 --> 00:00:25,430
So first I have console log that our server is listening on PT..

7
00:00:25,440 --> 00:00:29,310
Three thousand and only then we're connecting to the database.

8
00:00:29,880 --> 00:00:38,370
But if we think about it, what is really a use for our server if we are not connected to the database,

9
00:00:38,880 --> 00:00:41,570
whatever we're about to do is going to fail anyway.

10
00:00:42,520 --> 00:00:49,570
So what I'm trying to say is, wouldn't it make more sense if we tried to connect to database and only

11
00:00:49,570 --> 00:00:52,660
if we're successful when we spin up the server?

12
00:00:52,990 --> 00:00:53,710
Not at all.

13
00:00:53,740 --> 00:00:55,810
Then we'll just kill the application.

14
00:00:56,230 --> 00:01:02,950
And not to do that, we need to restructure our code a bit where I'm not going to invoke Mongoose Connect

15
00:01:03,400 --> 00:01:11,380
in the Connect Jazz, our fact or the code, and I'll set it up as a function and instead we will invoke

16
00:01:11,380 --> 00:01:13,500
it in the app Jass.

17
00:01:13,960 --> 00:01:19,710
And not to do that will just remove this dot then and catch now options.

18
00:01:19,720 --> 00:01:22,270
Of course, they were not doing anything with that.

19
00:01:22,600 --> 00:01:30,430
And here let's create a function or say Connect and DB and that is equal to our function.

20
00:01:30,460 --> 00:01:37,760
So essentially we're setting up the arrow function and eventually we'll set up the variable and effectively

21
00:01:37,810 --> 00:01:45,040
pass it from the objectives and therefore the parameter here, the URL for the time being, we won't

22
00:01:45,040 --> 00:01:45,430
use it.

23
00:01:45,680 --> 00:01:46,900
We'll do the same thing.

24
00:01:47,110 --> 00:01:53,260
What we did before where we just basking in the connection string that's coming up in the next video.

25
00:01:53,770 --> 00:02:00,150
And then I want to grab this Mongo's Connect, cut it out and learn from this Connect DB.

26
00:02:00,490 --> 00:02:02,250
I just want to return that result.

27
00:02:02,350 --> 00:02:06,130
So effectively what we're turning we're turning a promise.

28
00:02:06,130 --> 00:02:06,610
Correct.

29
00:02:07,000 --> 00:02:11,950
And then all the way in the bottom, we're going to go with module and exports.

30
00:02:11,950 --> 00:02:17,400
And now, of course, we want exported and what we want to export, of course, Connect and DB.

31
00:02:18,130 --> 00:02:24,250
So once we have this set up then we'll just have several years listening on PT..

32
00:02:24,250 --> 00:02:27,820
Three thousand, because now of course we want to sign it to some kind of variable.

33
00:02:28,210 --> 00:02:36,040
And again, the order is really up to you, but I'm just going to put it below the task, so I'll just

34
00:02:36,040 --> 00:02:41,770
move it over here below the tasks and I'll set a date is equal to connect DB.

35
00:02:42,190 --> 00:02:48,250
So effectively I'm just getting that function over here and I'll set it equal to the required one.

36
00:02:48,790 --> 00:02:55,030
And then once I have access to the function that all the way in the bottom I want to set up one more

37
00:02:55,030 --> 00:02:59,140
function by the name of start and end this function.

38
00:02:59,470 --> 00:03:06,180
We will invoke Connect DB and then only if we're successful then we'll open up the server.

39
00:03:06,190 --> 00:03:11,440
So let's go somewhere here in the bottom and I'll second start again when we use the arrow function

40
00:03:11,710 --> 00:03:13,210
and decide how that might connect.

41
00:03:13,210 --> 00:03:14,890
DB returns a promise.

42
00:03:15,370 --> 00:03:15,850
Correct?

43
00:03:16,330 --> 00:03:20,020
I can set this function as a sync and my way.

44
00:03:20,020 --> 00:03:26,230
Of course we can use the R word keyword and every time we have a synchronous operation, it's very useful

45
00:03:26,230 --> 00:03:34,770
to serve in the tri cashbook so that where if there is an error, we can handle it as well and in Trebilcock

46
00:03:34,780 --> 00:03:38,680
will pass in a weight so that it returns a promise.

47
00:03:38,680 --> 00:03:43,450
I'll use my a key word that I'm going to go with Karnack DB again.

48
00:03:43,450 --> 00:03:47,690
Eventually it'll pass in the string coming from the end.

49
00:03:47,740 --> 00:03:55,000
Refah for the time being we just have hardcoded in the correct address and then only if we're successful

50
00:03:55,270 --> 00:03:57,520
then we'll open up the server.

51
00:03:57,730 --> 00:04:05,500
So I'll move this line of code right below the Canarc DB and again, we'll just open up the server if

52
00:04:05,500 --> 00:04:07,450
the connection is successful.

53
00:04:07,690 --> 00:04:12,610
If not, we're going to go with the console log and then we'll just look for error.

54
00:04:12,880 --> 00:04:15,160
So once we save, nothing happens.

55
00:04:15,160 --> 00:04:19,029
We don't open up the server because of course, now we have a function.

56
00:04:19,329 --> 00:04:25,840
So all the way into bottom, I'm just going to go would start and then you'll notice that there's a

57
00:04:25,840 --> 00:04:27,550
console log for the server.

58
00:04:27,820 --> 00:04:33,250
Now, of course, we don't have the console log for the connection because we removed that and.

59
00:04:33,250 --> 00:04:33,550
Correct.

60
00:04:33,550 --> 00:04:40,420
Yes, but if you really wanted to start out, then can go back over here and just mess with your password

61
00:04:40,660 --> 00:04:45,850
and you will right away see that will get that error in console.

62
00:04:46,000 --> 00:04:49,810
And in here it says that auth authentication fail.

63
00:04:50,050 --> 00:04:51,910
So we know that our setup works.

64
00:04:52,180 --> 00:04:56,980
So if, of course, we will have a password, then we won't be successful.

65
00:04:56,980 --> 00:05:05,230
However, if everything goes smoothly, then we should see that our server is listening on four three

66
00:05:05,230 --> 00:05:05,680
thousand.

