Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,040 --> 00:00:05,250
So now that we're storing a newsletter successfully,
2
00:00:05,250 --> 00:00:08,580
let's also store and fetch our comments.
3
00:00:08,580 --> 00:00:12,070
For this I'll go back to the comments API route,
4
00:00:12,070 --> 00:00:16,410
and now here we also wanna connect with MongoDB,
5
00:00:16,410 --> 00:00:21,410
and then simply insert a new comment or fetch all comments.
6
00:00:23,250 --> 00:00:26,140
So either way, no matter what happens,
7
00:00:26,140 --> 00:00:28,890
we wanna connect to MongoDB.
8
00:00:28,890 --> 00:00:33,030
So before we dive into any if block here,
9
00:00:33,030 --> 00:00:34,860
I wanna set up my connection.
10
00:00:34,860 --> 00:00:36,670
Hence, we need to import
11
00:00:38,410 --> 00:00:39,260
MongoClient
12
00:00:40,963 --> 00:00:43,020
from mongodb here,
13
00:00:43,020 --> 00:00:45,440
and then repeat the code from before.
14
00:00:45,440 --> 00:00:47,320
And of course we definitely could also look
15
00:00:47,320 --> 00:00:49,640
into creating some helper functions,
16
00:00:49,640 --> 00:00:52,130
but I will just quickly repeat that.
17
00:00:52,130 --> 00:00:56,870
MongoClient connect, and then grab my connection string
18
00:00:56,870 --> 00:01:00,360
from the newsletter js file.
19
00:01:00,360 --> 00:01:01,593
Paste it in here.
20
00:01:02,640 --> 00:01:04,500
And again I'll use async await.
21
00:01:04,500 --> 00:01:08,260
So add async in front of the handler function,
22
00:01:08,260 --> 00:01:11,253
and await disconnection to get access to the client.
23
00:01:12,460 --> 00:01:17,460
Now already after all these if checks, I will go down there
24
00:01:17,580 --> 00:01:20,240
and already call client close to make sure
25
00:01:20,240 --> 00:01:21,990
that we don't forget this.
26
00:01:21,990 --> 00:01:25,680
That we close the connection once we're done with our logic
27
00:01:25,680 --> 00:01:28,183
no matter what our logic was.
28
00:01:29,640 --> 00:01:30,880
So now that's that.
29
00:01:30,880 --> 00:01:33,020
Now inside of request method POST,
30
00:01:33,020 --> 00:01:37,690
we wanna store our comment here in our database.
31
00:01:37,690 --> 00:01:41,090
And for this I'll create my new comment,
32
00:01:41,090 --> 00:01:44,530
but then thereafter, I will use the client
33
00:01:44,530 --> 00:01:47,910
and the db method to get access to the database
34
00:01:47,910 --> 00:01:49,290
to which we connected.
35
00:01:49,290 --> 00:01:50,320
And for that actually,
36
00:01:50,320 --> 00:01:52,740
I wanna adjust the connection string here,
37
00:01:52,740 --> 00:01:55,620
and not connect to a newsletter database,
38
00:01:55,620 --> 00:01:58,773
but instead to my comments database.
39
00:01:59,900 --> 00:02:04,450
Or, overall, we connect to our events database
40
00:02:04,450 --> 00:02:06,130
for the events application.
41
00:02:06,130 --> 00:02:09,070
And then we have our emails collection
42
00:02:09,070 --> 00:02:11,780
and we have our comments collection.
43
00:02:11,780 --> 00:02:14,280
And I think that makes more sense.
44
00:02:14,280 --> 00:02:16,480
So I'll connect to the events database
45
00:02:16,480 --> 00:02:19,400
in the newsletter js file as well,
46
00:02:19,400 --> 00:02:22,240
and connect to the same database here
47
00:02:22,240 --> 00:02:25,683
in the comments API route, events.
48
00:02:26,590 --> 00:02:29,600
So then it's this database which will work,
49
00:02:29,600 --> 00:02:33,870
and on that database, I then use the emails collection
50
00:02:33,870 --> 00:02:35,840
in the newsletter js file,
51
00:02:35,840 --> 00:02:39,300
or maybe let's now name the collection newsletter.
52
00:02:39,300 --> 00:02:40,850
But that's of course up to you.
53
00:02:41,760 --> 00:02:44,580
And in the comments event id file here,
54
00:02:44,580 --> 00:02:48,823
I'll connect or I'll work with a collection named comments.
55
00:02:50,580 --> 00:02:54,060
And here I now wanna insert one new comment.
56
00:02:54,060 --> 00:02:55,390
Now it is this comment,
57
00:02:55,390 --> 00:02:58,500
but I wanna tweak that comment object.
58
00:02:58,500 --> 00:03:01,410
I don't wanna create my dummy id anymore,
59
00:03:01,410 --> 00:03:04,880
because MongoDB will create a unique id for me.
60
00:03:04,880 --> 00:03:06,823
So I don't need to assign my own id.
61
00:03:07,660 --> 00:03:09,280
But instead I wanna do something
62
00:03:09,280 --> 00:03:11,550
which I haven't done up to this point.
63
00:03:11,550 --> 00:03:14,180
I want to use this event id which we're getting,
64
00:03:14,180 --> 00:03:15,580
which is encoded in the URL.
65
00:03:16,430 --> 00:03:17,680
And I wanna make it clear
66
00:03:17,680 --> 00:03:21,120
to which event my comment is related.
67
00:03:21,120 --> 00:03:22,760
Because in reality,
68
00:03:22,760 --> 00:03:27,410
we would probably manage our events with MongoDB as well.
69
00:03:27,410 --> 00:03:28,910
We're not doing that here,
70
00:03:28,910 --> 00:03:31,350
but in reality we would probably manage them
71
00:03:31,350 --> 00:03:33,100
in the database as well.
72
00:03:33,100 --> 00:03:36,500
So then, storing the id of the related document
73
00:03:36,500 --> 00:03:39,620
in some other collection in this document here
74
00:03:39,620 --> 00:03:41,340
would make a lot of sense.
75
00:03:41,340 --> 00:03:42,970
And that's definitely what I'll do.
76
00:03:42,970 --> 00:03:46,030
I'll store eventId as a extra property
77
00:03:46,030 --> 00:03:50,100
in the new comment to have this reference to the event
78
00:03:50,100 --> 00:03:51,913
to which this comment belongs.
79
00:03:53,050 --> 00:03:56,440
And then it's this new comment which I wanna insert.
80
00:03:56,440 --> 00:04:01,020
So here I'll insert one, and I'll insert this new comment.
81
00:04:01,020 --> 00:04:03,163
And of course await this operation.
82
00:04:04,170 --> 00:04:07,620
Now we do get back some response here by the way.
83
00:04:07,620 --> 00:04:11,640
Some result of that insert one operation,
84
00:04:11,640 --> 00:04:15,010
and I will console log that result for now.
85
00:04:15,010 --> 00:04:18,240
In the effort that's now my code here in the post case
86
00:04:18,240 --> 00:04:20,360
so that we add a new comment.
87
00:04:20,360 --> 00:04:23,950
Before we work on the get case here, let's save that code
88
00:04:23,950 --> 00:04:27,370
and let's see whether it works and makes sense.
89
00:04:27,370 --> 00:04:31,130
For this I'll dive into an individual event,
90
00:04:31,130 --> 00:04:33,250
show my comments, and add a new comment
91
00:04:34,200 --> 00:04:36,400
with some dummy data.
92
00:04:36,400 --> 00:04:39,373
This is a test with MongoDB.
93
00:04:40,980 --> 00:04:42,450
Click Submit.
94
00:04:42,450 --> 00:04:44,440
We still get no feedback here.
95
00:04:44,440 --> 00:04:45,273
We could add it,
96
00:04:45,273 --> 00:04:48,000
but we haven't done anything in that direction.
97
00:04:48,000 --> 00:04:50,800
But if I go the back end, that looks good.
98
00:04:50,800 --> 00:04:52,950
I got a output here.
99
00:04:52,950 --> 00:04:55,070
That's this result which I'm logging.
100
00:04:55,070 --> 00:04:56,000
Whoops.
101
00:04:56,000 --> 00:04:58,800
This result which I'm logging, that's what we see here.
102
00:04:58,800 --> 00:05:02,240
And we see that it's quite a long result.
103
00:05:02,240 --> 00:05:07,070
Quite a detailed output because result in case of insert one
104
00:05:07,070 --> 00:05:10,560
is actually containing full details
105
00:05:10,560 --> 00:05:14,370
about the operation that was performed,
106
00:05:14,370 --> 00:05:18,433
but then also the connection in general and much much more.
107
00:05:20,130 --> 00:05:23,090
Now what we can see if we go to the cluster
108
00:05:23,090 --> 00:05:28,090
is that here if we reload this page to refresh the data,
109
00:05:28,980 --> 00:05:32,370
we got this events database now with a comments collection,
110
00:05:32,370 --> 00:05:36,240
and in there we indeed see that comment which I just added.
111
00:05:36,240 --> 00:05:40,500
So it was stored successfully in the database.
112
00:05:40,500 --> 00:05:43,390
Now what's also interesting on the result
113
00:05:43,390 --> 00:05:47,840
is that on the result we also get back that unique id
114
00:05:47,840 --> 00:05:50,270
which was generated automatically.
115
00:05:50,270 --> 00:05:53,060
That's interesting because that means that
116
00:05:53,060 --> 00:05:56,610
on the new comment object, which we stored in the database,
117
00:05:56,610 --> 00:05:59,030
we can now add a id field
118
00:05:59,030 --> 00:06:02,350
and set this equal to result.insertedId.
119
00:06:04,740 --> 00:06:06,830
So that the new comment which we sent back
120
00:06:06,830 --> 00:06:09,760
to the front end also contains this unique id
121
00:06:09,760 --> 00:06:11,320
that was generated.
122
00:06:11,320 --> 00:06:14,830
I guess that's convenient to have in case we would work
123
00:06:14,830 --> 00:06:18,373
with that inserted comment on the front end right away.
124
00:06:19,630 --> 00:06:21,350
So that's now the post case.
125
00:06:21,350 --> 00:06:24,823
Let's now work on the get method where we get our comments.
9784
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.