1
00:00:02,600 --> 00:00:04,600
- [Maximilian] Now to
conclude this section

2
00:00:04,600 --> 00:00:08,800
about working with MongoDB
and Node and Express,

3
00:00:08,800 --> 00:00:12,100
I wanna highlight a specific
limitation and issue

4
00:00:12,100 --> 00:00:16,866
that exists in Express.js of
which you should be aware.

5
00:00:16,866 --> 00:00:20,000
When you have asynchronous route handler

6
00:00:20,000 --> 00:00:22,033
or route middleware functions,

7
00:00:22,033 --> 00:00:23,500
as I have it here, for example,

8
00:00:23,500 --> 00:00:26,233
for getting a specific post by ID,

9
00:00:26,233 --> 00:00:31,233
then whenever an error is thrown
inside of such a function,

10
00:00:31,533 --> 00:00:35,066
the default Express error
handling middleware,

11
00:00:35,066 --> 00:00:38,033
which normally catches errors for us,

12
00:00:38,033 --> 00:00:39,566
will not become active.

13
00:00:40,700 --> 00:00:43,433
Now because of that,
you will actually notice

14
00:00:43,433 --> 00:00:46,866
that if you will try
to view a specific post

15
00:00:46,866 --> 00:00:51,866
and you manually enter a
wrong ID here, it crashes.

16
00:00:52,500 --> 00:00:54,433
This entire server crashes

17
00:00:54,433 --> 00:00:57,066
instead of showing us the 404 page.

18
00:00:58,100 --> 00:01:00,600
The reason for that is
that we don't even make it

19
00:01:00,600 --> 00:01:04,800
to this part here because
creating such an object ID

20
00:01:04,800 --> 00:01:08,600
for which we're using a
functionality exposed by MongoDB

21
00:01:08,600 --> 00:01:12,333
will actually cause an
error and throw an error

22
00:01:12,333 --> 00:01:14,533
if we don't pass in a valid ID

23
00:01:14,533 --> 00:01:17,833
that can be converted
to such an object ID.

24
00:01:17,833 --> 00:01:20,333
That's just how the MongoDB package works

25
00:01:20,333 --> 00:01:23,266
when it comes to the
creation of these object IDs.

26
00:01:24,166 --> 00:01:26,100
Now, if an error is thrown,

27
00:01:26,100 --> 00:01:29,233
normally the default error handling route

28
00:01:29,233 --> 00:01:30,400
would become active.

29
00:01:30,400 --> 00:01:33,133
This error handling
middleware to be precise.

30
00:01:34,200 --> 00:01:36,533
But since we're in an async function

31
00:01:36,533 --> 00:01:38,066
and therefore in a function

32
00:01:38,066 --> 00:01:40,200
that automatically returns a promise

33
00:01:40,200 --> 00:01:43,100
because all async
functions return promises,

34
00:01:43,100 --> 00:01:45,266
that's what the async keyword does,

35
00:01:45,266 --> 00:01:47,700
it wraps the entire
content of the function

36
00:01:47,700 --> 00:01:50,200
into a promise so to say,

37
00:01:50,200 --> 00:01:51,766
because that's the case,

38
00:01:51,766 --> 00:01:54,966
Express.js, because of
its mentioned limitation,

39
00:01:54,966 --> 00:01:57,333
does not catch this error for us.

40
00:01:58,366 --> 00:02:00,600
To ensure that everything still works,

41
00:02:00,600 --> 00:02:02,900
we have to do that manually instead.

42
00:02:02,900 --> 00:02:05,633
We can use the try catch feature

43
00:02:05,633 --> 00:02:08,633
I mentioned early in the course already.

44
00:02:08,633 --> 00:02:11,533
Here, we can try a certain piece of code.

45
00:02:11,533 --> 00:02:14,666
For example, we can try creating this ID

46
00:02:16,166 --> 00:02:17,200
like this.

47
00:02:17,200 --> 00:02:18,833
And if that fails,

48
00:02:18,833 --> 00:02:23,200
we can catch the error and come
up with some fallback plan.

49
00:02:24,333 --> 00:02:27,500
So we could then actually change

50
00:02:27,500 --> 00:02:30,300
the postId here to a variable

51
00:02:30,300 --> 00:02:33,666
so that we can reassign it
here if that should succeed

52
00:02:33,666 --> 00:02:36,166
and then we can use postId down here

53
00:02:36,166 --> 00:02:38,133
if converting had succeeded.

54
00:02:38,133 --> 00:02:39,300
But if that should fail

55
00:02:39,300 --> 00:02:42,366
because we, for example,
passed in a wrong postId,

56
00:02:42,366 --> 00:02:46,433
then we can catch the error
manually and handle it manually,

57
00:02:46,433 --> 00:02:48,900
and we have to do that
because as mentioned,

58
00:02:48,900 --> 00:02:52,433
the default error handling
middleware won't become active.

59
00:02:53,500 --> 00:02:57,266
Now, there are two ways of
handling it manually here.

60
00:02:57,266 --> 00:03:01,033
You can directly return the
response you wanna return,

61
00:03:01,033 --> 00:03:03,133
for example, that 404 page.

62
00:03:03,133 --> 00:03:04,566
We can do that here.

63
00:03:06,200 --> 00:03:08,866
And that is probably
what we wanna do here.

64
00:03:09,833 --> 00:03:12,333
But alternatively, if you wanna ensure

65
00:03:12,333 --> 00:03:16,166
that the default error handling
function becomes active,

66
00:03:16,166 --> 00:03:20,566
you can accept a third
parameter here in this function

67
00:03:20,566 --> 00:03:22,566
and that's this next parameter.

68
00:03:23,466 --> 00:03:26,566
Express.js provides this next parameter

69
00:03:26,566 --> 00:03:28,833
for all the middleware functions

70
00:03:28,833 --> 00:03:31,500
and the route handling
function here, in the end,

71
00:03:31,500 --> 00:03:33,633
is just a middleware function.

72
00:03:34,566 --> 00:03:38,133
Next is itself a function
that can be executed

73
00:03:38,133 --> 00:03:40,800
and we can execute it to move the request

74
00:03:40,800 --> 00:03:43,166
on to the next middleware in line.

75
00:03:44,033 --> 00:03:45,766
Now, in these middleware functions

76
00:03:45,766 --> 00:03:48,500
that we use in our routes setup here,

77
00:03:48,500 --> 00:03:50,300
we typically don't wanna do that

78
00:03:50,300 --> 00:03:53,400
because these route
functions are the last step

79
00:03:53,400 --> 00:03:56,033
in handling the request.

80
00:03:56,033 --> 00:03:59,000
But if things go wrong,
that's actually not the case,

81
00:03:59,000 --> 00:04:01,533
then I want the request to move on

82
00:04:01,533 --> 00:04:04,000
to the default error handling middleware.

83
00:04:04,000 --> 00:04:06,233
And to ensure that this is the case,

84
00:04:06,233 --> 00:04:10,433
all we have to do is pass
this error object into next.

85
00:04:11,333 --> 00:04:13,700
If we do that and then return

86
00:04:13,700 --> 00:04:17,000
to make sure that no other
code executes thereafter,

87
00:04:17,000 --> 00:04:18,733
we will actually again reach

88
00:04:18,733 --> 00:04:21,866
the default error handling middleware.

89
00:04:21,866 --> 00:04:25,100
So if I now save everything and reload,

90
00:04:25,100 --> 00:04:27,500
now I get the something went wrong page

91
00:04:27,500 --> 00:04:29,633
instead of the server crashing.

92
00:04:31,266 --> 00:04:34,333
Again here, that's probably
not even what we wanna do.

93
00:04:34,333 --> 00:04:38,933
We might instead prefer to just
directly render to 404 page.

94
00:04:38,933 --> 00:04:41,533
And of course, we can do that as well.

95
00:04:41,533 --> 00:04:43,500
Now we get this page if I reload.

96
00:04:44,733 --> 00:04:46,700
It's just important that you understand

97
00:04:46,700 --> 00:04:50,766
that when you work with
asynchronous code with promises,

98
00:04:50,766 --> 00:04:53,566
you have to manually forward errors

99
00:04:53,566 --> 00:04:56,833
to the default error handling
middleware, as I showed you,

100
00:04:56,833 --> 00:04:59,966
or handle the error manually in general,

101
00:04:59,966 --> 00:05:03,033
for example, by setting your own response.

102
00:05:03,033 --> 00:05:06,500
And theoretically, we
should manually handle

103
00:05:06,500 --> 00:05:09,366
all possible errors that could occur

104
00:05:09,366 --> 00:05:12,833
in any of the other route
handlers here as well.

105
00:05:12,833 --> 00:05:16,633
For example, whenever we
are converting some ID

106
00:05:16,633 --> 00:05:18,800
to an object ID.

107
00:05:18,800 --> 00:05:22,166
I'll not do it here to not
clutter the code too much,

108
00:05:22,166 --> 00:05:24,700
but you can do this as an extra practice.

109
00:05:24,700 --> 00:05:28,300
And later when we build this
main online shop project,

110
00:05:28,300 --> 00:05:30,700
we will, of course, do it properly there

111
00:05:30,700 --> 00:05:34,033
so that there you see
a best practice project

112
00:05:34,033 --> 00:05:35,300
from the ground up.

113
00:05:36,166 --> 00:05:39,466
But here, I just want to make
you aware of this limitation

114
00:05:39,466 --> 00:05:41,466
and that in async functions,

115
00:05:41,466 --> 00:05:44,466
you have to manually
handle potential errors

116
00:05:44,466 --> 00:05:47,133
that could occur because otherwise,

117
00:05:47,133 --> 00:05:50,466
the server will just crash
and we don't want that.

118
00:05:52,533 --> 00:05:55,700
And therefore, that's now
this project finished.

119
00:05:55,700 --> 00:05:58,933
Now we added all these
features we wanted to add

120
00:05:58,933 --> 00:06:00,066
with help of MongoDB.

121
00:06:01,033 --> 00:06:04,266
And now the data is
stored in a real database,

122
00:06:04,266 --> 00:06:07,266
in the MongoDB database to be precise,

123
00:06:07,266 --> 00:06:10,300
and we saw all these
core MongoDB operations

124
00:06:10,300 --> 00:06:13,500
which I introduced you to
in the last course section

125
00:06:13,500 --> 00:06:16,533
in action in this course section now.

126
00:06:16,533 --> 00:06:18,966
And therefore, now we got
all the basics we need

127
00:06:18,966 --> 00:06:22,000
to also use that a little
bit later in the course

128
00:06:22,000 --> 00:06:24,100
once we work on a bigger project.

129
00:06:24,933 --> 00:06:28,766
Of course, as always, definitely
feel free to practice this

130
00:06:28,766 --> 00:06:30,566
even a bit more on your own.

131
00:06:30,566 --> 00:06:34,400
Maybe try managing authors
on a brand new page here,

132
00:06:34,400 --> 00:06:36,666
build your own demo projects,

133
00:06:36,666 --> 00:06:38,866
and of course also go through this

134
00:06:38,866 --> 00:06:41,033
and/or the last section again

135
00:06:41,033 --> 00:06:43,400
if some of the aspects covered here

136
00:06:43,400 --> 00:06:46,466
should not be entirely clear yet

137
00:06:46,466 --> 00:06:48,600
because we are now going to move on

138
00:06:48,600 --> 00:06:50,933
and dive deeper into web development

139
00:06:50,933 --> 00:06:52,666
in the next course sections.

