Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So now that we're storing a newsletter successfully,
let's also store and fetch our comments.
For this I'll go back to the comments API route,
and now here we also wanna connect with MongoDB,
and then simply insert a new comment or fetch all comments.
So either way, no matter what happens,
we wanna connect to MongoDB.
So before we dive into any if block here,
I wanna set up my connection.
Hence, we need to import
MongoClient
from mongodb here,
and then repeat the code from before.
And of course we definitely could also look
into creating some helper functions,
but I will just quickly repeat that.
MongoClient connect, and then grab my connection string
from the newsletter js file.
Paste it in here.
And again I'll use async await.
So add async in front of the handler function,
and await disconnection to get access to the client.
Now already after all these if checks, I will go down there
and already call client close to make sure
that we don't forget this.
That we close the connection once we're done with our logic
no matter what our logic was.
So now that's that.
Now inside of request method POST,
we wanna store our comment here in our database.
And for this I'll create my new comment,
but then thereafter, I will use the client
and the db method to get access to the database
to which we connected.
And for that actually,
I wanna adjust the connection string here,
and not connect to a newsletter database,
but instead to my comments database.
Or, overall, we connect to our events database
for the events application.
And then we have our emails collection
and we have our comments collection.
And I think that makes more sense.
So I'll connect to the events database
in the newsletter js file as well,
and connect to the same database here
in the comments API route, events.
So then it's this database which will work,
and on that database, I then use the emails collection
in the newsletter js file,
or maybe let's now name the collection newsletter.
But that's of course up to you.
And in the comments event id file here,
I'll connect or I'll work with a collection named comments.
And here I now wanna insert one new comment.
Now it is this comment,
but I wanna tweak that comment object.
I don't wanna create my dummy id anymore,
because MongoDB will create a unique id for me.
So I don't need to assign my own id.
But instead I wanna do something
which I haven't done up to this point.
I want to use this event id which we're getting,
which is encoded in the URL.
And I wanna make it clear
to which event my comment is related.
Because in reality,
we would probably manage our events with MongoDB as well.
We're not doing that here,
but in reality we would probably manage them
in the database as well.
So then, storing the id of the related document
in some other collection in this document here
would make a lot of sense.
And that's definitely what I'll do.
I'll store eventId as a extra property
in the new comment to have this reference to the event
to which this comment belongs.
And then it's this new comment which I wanna insert.
So here I'll insert one, and I'll insert this new comment.
And of course await this operation.
Now we do get back some response here by the way.
Some result of that insert one operation,
and I will console log that result for now.
In the effort that's now my code here in the post case
so that we add a new comment.
Before we work on the get case here, let's save that code
and let's see whether it works and makes sense.
For this I'll dive into an individual event,
show my comments, and add a new comment
with some dummy data.
This is a test with MongoDB.
Click Submit.
We still get no feedback here.
We could add it,
but we haven't done anything in that direction.
But if I go the back end, that looks good.
I got a output here.
That's this result which I'm logging.
Whoops.
This result which I'm logging, that's what we see here.
And we see that it's quite a long result.
Quite a detailed output because result in case of insert one
is actually containing full details
about the operation that was performed,
but then also the connection in general and much much more.
Now what we can see if we go to the cluster
is that here if we reload this page to refresh the data,
we got this events database now with a comments collection,
and in there we indeed see that comment which I just added.
So it was stored successfully in the database.
Now what's also interesting on the result
is that on the result we also get back that unique id
which was generated automatically.
That's interesting because that means that
on the new comment object, which we stored in the database,
we can now add a id field
and set this equal to result.insertedId.
So that the new comment which we sent back
to the front end also contains this unique id
that was generated.
I guess that's convenient to have in case we would work
with that inserted comment on the front end right away.
So that's now the post case.
Let's now work on the get method where we get our comments.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.