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