1
00:00:02,000 --> 00:00:04,000
There we can do something similar.

2
00:00:04,000 --> 00:00:05,000
We can create separate functions

3
00:00:05,000 --> 00:00:07,000
for connecting and inserting

4
00:00:07,000 --> 00:00:11,000
and since we would be reusing the same logic,

5
00:00:11,000 --> 00:00:15,000
we might wanna go for some helper functions instead.

6
00:00:15,000 --> 00:00:17,000
We got that helpers folder anyways

7
00:00:17,000 --> 00:00:20,000
with some API utilities

8
00:00:20,000 --> 00:00:24,000
and there we can add a db-util.js file

9
00:00:24,000 --> 00:00:28,000
to set up our database utility functions.

10
00:00:28,000 --> 00:00:30,000
And I'll then go to the newsletter.js file

11
00:00:30,000 --> 00:00:34,000
and grab the connectDatabase and insertDocument functions,

12
00:00:34,000 --> 00:00:36,000
cut them from newsletter.js

13
00:00:36,000 --> 00:00:40,000
and instead add them in db-util

14
00:00:40,000 --> 00:00:44,000
and just change the insertDocument function

15
00:00:44,000 --> 00:00:49,000
such that it also takes the collection as a parameter

16
00:00:49,000 --> 00:00:50,000
so that we don't always talk

17
00:00:50,000 --> 00:00:53,000
to the newsletter collection here

18
00:00:53,000 --> 00:00:56,000
but to any collection we get as a parameter.

19
00:00:57,000 --> 00:01:01,000
Now we wanna export both functions

20
00:01:01,000 --> 00:01:04,000
so that we can import them into our files

21
00:01:04,000 --> 00:01:07,000
and then we can use them here in newsletter.js.

22
00:01:07,000 --> 00:01:12,000
There we can now import from our db-util file

23
00:01:14,000 --> 00:01:18,000
by going up to the helpers/db-util

24
00:01:18,000 --> 00:01:21,000
and here I'll import connectDatabase and insertDocument

25
00:01:23,000 --> 00:01:26,000
and for insertDocument, we now just also need

26
00:01:26,000 --> 00:01:28,000
to pass in that collection name

27
00:01:28,000 --> 00:01:32,000
as a second argument so that the email is inserted

28
00:01:32,000 --> 00:01:35,000
into the correct collection.

29
00:01:35,000 --> 00:01:38,000
And with that, we can go to the eventId file here

30
00:01:38,000 --> 00:01:42,000
and also import these two functions here.

31
00:01:42,000 --> 00:01:46,000
So from the helpers folder.

32
00:01:47,000 --> 00:01:50,000
We wanna import from the db-util file

33
00:01:50,000 --> 00:01:53,000
and here we also wanna import connectDatabase

34
00:01:54,000 --> 00:01:57,000
and insertDocument and then use that.

35
00:01:57,000 --> 00:02:00,000
Here, for example, we connect to the database,

36
00:02:00,000 --> 00:02:04,000
so here we wanna await connectDatabase like this

37
00:02:04,000 --> 00:02:07,000
to get access to the client.

38
00:02:07,000 --> 00:02:08,000
Now, this could be failing,

39
00:02:08,000 --> 00:02:09,000
so just as before,

40
00:02:09,000 --> 00:02:12,000
I will wrap it in a try catch block.

41
00:02:13,000 --> 00:02:17,000
Catch any error we might be getting like that

42
00:02:18,000 --> 00:02:22,000
and create client as a variable,

43
00:02:22,000 --> 00:02:23,000
which we then set in here.

44
00:02:24,000 --> 00:02:27,000
And then in here, we wanna send back a response

45
00:02:27,000 --> 00:02:29,000
with a status code of 500

46
00:02:29,000 --> 00:02:32,000
and some message in the JSON body

47
00:02:32,000 --> 00:02:37,000
that could be connecting to the database failed.

48
00:02:39,000 --> 00:02:42,000
Like this, and then still return

49
00:02:42,000 --> 00:02:45,000
to avoid that the rest of this function executes.

50
00:02:46,000 --> 00:02:49,000
If we do connect successfully down here,

51
00:02:49,000 --> 00:02:50,000
we're inserting the comment,

52
00:02:50,000 --> 00:02:53,000
we don't wanna run this code here.

53
00:02:55,000 --> 00:02:58,000
Instead we wanna execute insertDocument,

54
00:03:00,000 --> 00:03:02,000
pass the client to insertDocument,

55
00:03:02,000 --> 00:03:05,000
use the comments collection

56
00:03:05,000 --> 00:03:08,000
and pass the newComment as a document

57
00:03:08,000 --> 00:03:09,000
that should be inserted.

58
00:03:09,000 --> 00:03:14,000
And await to get access to the result.

59
00:03:14,000 --> 00:03:17,000
And for that, we wanna make sure that in db-util,

60
00:03:17,000 --> 00:03:20,000
we actually do return that result

61
00:03:20,000 --> 00:03:23,000
so that here we also grab that result

62
00:03:23,000 --> 00:03:25,000
and return it like that.

63
00:03:27,000 --> 00:03:29,000
With that, we can insert the document

64
00:03:29,000 --> 00:03:32,000
and still use the result to then update the ID.

65
00:03:32,000 --> 00:03:36,000
Here we, by the way, also might wanna use _id

66
00:03:36,000 --> 00:03:40,000
to be in line with the automatically generated field

67
00:03:40,000 --> 00:03:43,000
because there the field name is also _id

68
00:03:43,000 --> 00:03:45,000
as you saw in the last lectures.

69
00:03:46,000 --> 00:03:49,000
Now, the main goal was to add error handling though

70
00:03:49,000 --> 00:03:53,000
and hence, I'll wrap this into a try catch block as well.

71
00:03:53,000 --> 00:03:55,000
Catch any potential errors,

72
00:03:55,000 --> 00:03:59,000
return to not execute any other code if that did fail

73
00:03:59,000 --> 00:04:03,000
and then send back a status code of 500

74
00:04:03,000 --> 00:04:05,000
and some response data

75
00:04:05,000 --> 00:04:07,000
where we again could add a message

76
00:04:07,000 --> 00:04:10,000
of Inserting comment failed.

77
00:04:11,000 --> 00:04:14,000
And now I just want to create result as a variable,

78
00:04:14,000 --> 00:04:17,000
which I actually set here to get scoping right.

79
00:04:19,000 --> 00:04:23,000
Now the only remaining part is where we get our data.

80
00:04:23,000 --> 00:04:26,000
We can also create a helper function for that

81
00:04:26,000 --> 00:04:28,000
even though we only use it here

82
00:04:28,000 --> 00:04:30,000
but still to have a uniform approach

83
00:04:30,000 --> 00:04:33,000
when it comes to working with the database.

84
00:04:33,000 --> 00:04:36,000
I will go to the db-util file

85
00:04:36,000 --> 00:04:38,000
and export another async function.

86
00:04:38,000 --> 00:04:42,000
getAllDocuments sounds like a fitting name.

87
00:04:44,000 --> 00:04:46,000
And here I also need the client

88
00:04:46,000 --> 00:04:50,000
and the collection from which I wanna get all documents.

89
00:04:50,000 --> 00:04:52,000
And then we can take this code

90
00:04:52,000 --> 00:04:55,000
where we get access to the database

91
00:04:55,000 --> 00:04:58,000
and then fetch all entries and comments

92
00:04:58,000 --> 00:05:02,000
and paste that into getAllDocuments.

93
00:05:03,000 --> 00:05:07,000
Now actually we might wanna accept sort,

94
00:05:07,000 --> 00:05:10,000
this sort object as a argument as well

95
00:05:10,000 --> 00:05:12,000
to make this function more flexible

96
00:05:12,000 --> 00:05:17,000
and let the caller of the function specify how documents

97
00:05:17,000 --> 00:05:18,000
should be sorted.

98
00:05:19,000 --> 00:05:21,000
Then we wanna use collection here

99
00:05:21,000 --> 00:05:25,000
and use the sort parameter here

100
00:05:25,000 --> 00:05:29,000
and ultimately, of course, return our documents.

101
00:05:29,000 --> 00:05:29,000
Like this.

102
00:05:31,000 --> 00:05:34,000
Now in the eventId file,

103
00:05:34,000 --> 00:05:38,000
we can import this newly added getAllDocuments function

104
00:05:38,000 --> 00:05:43,000
from the db-util file and use it down there.

105
00:05:43,000 --> 00:05:48,000
Here in the GET case, I wanna call getAllDocuments,

106
00:05:50,000 --> 00:05:53,000
pass in my client, pass in my collection name,

107
00:05:53,000 --> 00:05:54,000
which is comments

108
00:05:54,000 --> 00:05:56,000
and pass in my sort object,

109
00:05:56,000 --> 00:06:01,000
which defines that I wanna sort by id in descending order.

110
00:06:02,000 --> 00:06:04,000
Then we can remove this

111
00:06:04,000 --> 00:06:06,000
and await getAllDocuments

112
00:06:06,000 --> 00:06:10,000
to get all our documents here like this

113
00:06:10,000 --> 00:06:13,000
and return our response.

114
00:06:13,000 --> 00:06:15,000
But again, we wanna add error handling

115
00:06:15,000 --> 00:06:18,000
so we can try this and catch any errors

116
00:06:18,000 --> 00:06:20,000
if something goes wrong here.

117
00:06:20,000 --> 00:06:24,000
In which case, we stop function execution with return

118
00:06:24,000 --> 00:06:26,000
and then return a different response

119
00:06:26,000 --> 00:06:29,000
where the status code is set to 500

120
00:06:29,000 --> 00:06:32,000
and we pass on some message.

121
00:06:32,000 --> 00:06:37,000
For example, here we could say Getting comments failed.

122
00:06:38,000 --> 00:06:41,000
Now, you will be able to refactor more.

123
00:06:41,000 --> 00:06:44,000
For example, sending back that error response

124
00:06:44,000 --> 00:06:46,000
is always pretty much the same.

125
00:06:46,000 --> 00:06:49,000
You could also create a separate function for that,

126
00:06:49,000 --> 00:06:50,000
just as a side note.

127
00:06:52,000 --> 00:06:54,000
Now, with that, we get our documents here

128
00:06:54,000 --> 00:06:58,000
and I wanna send back a response here if that all works,

129
00:06:58,000 --> 00:07:02,000
otherwise we send back that error response.

130
00:07:02,000 --> 00:07:04,000
And we then close the client

131
00:07:04,000 --> 00:07:08,000
and actually now that I move to this success response

132
00:07:08,000 --> 00:07:11,000
into the try block, we can remove this return statement

133
00:07:11,000 --> 00:07:14,000
to make sure that we do close the client

134
00:07:14,000 --> 00:07:17,000
if we make it past try catch here.

135
00:07:17,000 --> 00:07:22,000
So to make sure that we do always get rid of that connection

136
00:07:22,000 --> 00:07:25,000
once we're done with our operations.

137
00:07:25,000 --> 00:07:27,000
And actually, I wanna do something similar here

138
00:07:27,000 --> 00:07:29,000
for inserting a comment.

139
00:07:29,000 --> 00:07:32,000
Move this success code into the try block

140
00:07:32,000 --> 00:07:35,000
and then don't return here

141
00:07:35,000 --> 00:07:39,000
so that we still do reach this part at the bottom eventually

142
00:07:39,000 --> 00:07:42,000
where we do close the connection.

143
00:07:42,000 --> 00:07:44,000
That's why I like this way of handling it better.

144
00:07:45,000 --> 00:07:48,000
I also wanna close the connection here

145
00:07:48,000 --> 00:07:50,000
if we have invalid input.

146
00:07:50,000 --> 00:07:52,000
So I'll copy client.close

147
00:07:52,000 --> 00:07:54,000
and add it in here

148
00:07:54,000 --> 00:07:56,000
because here I then wanna return.

149
00:07:56,000 --> 00:07:58,000
I don't wanna continue with code execution.

150
00:07:58,000 --> 00:08:00,000
Hence here, I wanna close the client,

151
00:08:00,000 --> 00:08:02,000
so close the connection.

152
00:08:02,000 --> 00:08:05,000
And here we don't wanna close the connection

153
00:08:05,000 --> 00:08:08,000
because here establishing the connection failed already.

154
00:08:08,000 --> 00:08:10,000
So here we definitely wanna return.

155
00:08:11,000 --> 00:08:13,000
And with all of that,

156
00:08:13,000 --> 00:08:15,000
we can again test this.

157
00:08:15,000 --> 00:08:18,000
Again by breaking our credentials.

158
00:08:18,000 --> 00:08:21,000
Then go to a single event.

159
00:08:22,000 --> 00:08:24,000
And show the comments.

160
00:08:24,000 --> 00:08:26,000
And fetching them fails now.

161
00:08:26,000 --> 00:08:30,000
And this breaks the front end application actually

162
00:08:30,000 --> 00:08:33,000
because now we're trying to map through all comments,

163
00:08:33,000 --> 00:08:35,000
even though we got no comments.

164
00:08:35,000 --> 00:08:38,000
So we might wanna add better error handling

165
00:08:38,000 --> 00:08:39,000
on the front end as well.

166
00:08:39,000 --> 00:08:41,000
For the moment, I'll just fix it

167
00:08:41,000 --> 00:08:44,000
by bringing back my credentials and reloading.

168
00:08:45,000 --> 00:08:49,000
Now this still fails.

169
00:08:49,000 --> 00:08:52,000
So it seems like something is wrong.

170
00:08:53,000 --> 00:08:55,000
Now let's see what's wrong by reloading

171
00:08:55,000 --> 00:08:57,000
with the Network tab open

172
00:08:58,000 --> 00:09:00,000
and then if I click on Show Comments,

173
00:09:00,000 --> 00:09:02,000
this request still fails,

174
00:09:02,000 --> 00:09:05,000
connecting to the database failed here

175
00:09:05,000 --> 00:09:08,000
even though I've fixed my credentials again.

176
00:09:09,000 --> 00:09:10,000
Even though I did that,

177
00:09:10,000 --> 00:09:15,000
it looks like calling connectDatabase failed

178
00:09:15,000 --> 00:09:17,000
in my handler function here.

179
00:09:17,000 --> 00:09:20,000
And actually, I know what went wrong.

180
00:09:20,000 --> 00:09:23,000
I moved my function here,

181
00:09:23,000 --> 00:09:26,000
my helper functions here into the db-util file

182
00:09:26,000 --> 00:09:29,000
but there I'm referring MongoClient.

183
00:09:29,000 --> 00:09:30,000
We need to add that import.

184
00:09:30,000 --> 00:09:33,000
So we should grab that MongoClient import

185
00:09:33,000 --> 00:09:35,000
and remove it from the newsletter file.

186
00:09:35,000 --> 00:09:37,000
We don't need it there anymore.

187
00:09:37,000 --> 00:09:40,000
Same is true for the eventId file

188
00:09:40,000 --> 00:09:42,000
in the comments folder.

189
00:09:42,000 --> 00:09:43,000
We can get rid of it there

190
00:09:43,000 --> 00:09:47,000
but we should add this import in the db-util.js file

191
00:09:47,000 --> 00:09:51,000
because we are using MongoClient there.

192
00:09:51,000 --> 00:09:52,000
That's why connecting failed.

193
00:09:52,000 --> 00:09:55,000
I was referring to MongoClient

194
00:09:55,000 --> 00:09:57,000
without having it available.

195
00:09:58,000 --> 00:10:00,000
So now with that, if we now reload,

196
00:10:03,000 --> 00:10:05,000
loading the comments works

197
00:10:05,000 --> 00:10:09,000
and if I now break my credentials here and save this,

198
00:10:10,000 --> 00:10:12,000
if I now try to submit

199
00:10:15,000 --> 00:10:16,000
a new comment,

200
00:10:18,000 --> 00:10:21,000
this should fail and it does.

201
00:10:21,000 --> 00:10:22,000
So that works.

202
00:10:22,000 --> 00:10:24,000
Again, no feedback on the front end.

203
00:10:24,000 --> 00:10:26,000
We could add it, we don't have it here.

204
00:10:26,000 --> 00:10:29,000
If I bring back my correct credentials

205
00:10:29,000 --> 00:10:33,000
and I try the same data again, this now worked.

206
00:10:33,000 --> 00:10:36,000
If I now hide the comments and show them again,

207
00:10:36,000 --> 00:10:40,000
we see that both comments are loaded as well.

208
00:10:40,000 --> 00:10:42,000
So that is all working here

209
00:10:42,000 --> 00:10:46,000
and we now did also add error handling therefore.

