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
Now, as a database,
I will use MongoDB here and to be precise,
I'll use their cloud offering, MongoDB Atlas,
which is a fully managed cloud-based MongoDB database
with which we can get started for free.
All you need to do is click Try Free.
This will then get you to MongoDB Atlas in the end
and create an account for free here.
So just enter some data about yourself
and then get started for free.
If you do all of that,
after logging in, you'll end up on a page
where you can create a new cluster.
You must also automatically end up
in that create a cluster wizard.
And here you can generally leave all the settings
as they are.
Just make sure that you always pick the free tier settings
unless you wanna pay.
So for example, you can go with AWS
and the default region here.
And leave all the other default settings as well.
Especially make sure that you're using the M0 Sandbox here.
I can't select this here
because I already do have a cluster created based
on that sandbox but if you don't have a cluster yet,
you should be able to select M0 Sandbox.
And that is the cluster tier you should use then.
And then just leave all the other default settings
and create your cluster.
Again, I already have one based on that M0 Sandbox here
and therefore, you eventually should also have one.
Now, that's the cluster, the MongoDB database cluster,
which we'll use to connect to
to then create our database in there
and to store our data in there.
Now, in order to be able to connect to this cluster,
there are two things you also have to do here
in this MongoDB Atlas console.
Under Network Access,
you have to add your local IP here
by clicking on Add IP Address
and then selecting current IP
so that your local computer is able
to send requests to MongoDB
and under Database Access,
you need to create at least one user
with any username of your choice
who has to have read and write access to the database,
like this user here does
so that this is a user with which we can connect
to this cluster later.
And once you did all of that,
on Clusters, you can click on Connect
and then connect your application
to learn how you can connect to this cluster.
And here we now actually need
to use the Node.js MongoDB driver
to write code here in our APIi route
that does send queries to this MongoDB Atlas cluster.
For this, we need to quit the development server
and install an extra package with npm install.
The mongodb package.
This is the official MongoDB driver,
which makes sending queries to MongoDB easy.
Once you install it,
you can start the development server again.
And now this MongoDB driver allows us
to connect to this cluster
and then insert data or fetch data from there.
For this in this api route file,
we can import something from mongodb
and that something is the MongoClient object.
This is our object which allows us to connect.
We can execute this code here.
We can use MongoClient and call the connect method here.
Now, the connect method wants this connection string,
which we have here.
So you should copy that string
and then pass this as a parameter here
to the connect method.
Now, there, you need to plug in the username
and password of your user.
So that user who has read and write access,
which you created.
And I'll quickly do this
without the angle brackets like this.
Now, I will have changed my password
when you're viewing this
so you don't need to try to connect to my cluster.
You also should replace myFirstDatabase
with any database name of your choice, for example, meetups,
but that is up to you.
Now, when you do that, connect will establish a connection.
And this is code which you never, never want
to run on the client side
because you would expose your credentials to your visitors,
which is a security problem.
But here in this API route, it's no problem
because as I mentioned before,
the code defined in here
will never end up on the client side
so this is a secure place to store credentials.
Now, connect does return a promise
and hence, we can turn our handler function
into a async function to use await.
This is possible and supported by NextJS.
And then this will give us a connected client eventually.
Now, on that client object, we can all the db method
to get hold of that database to which we're connecting here.
By the way, if that database doesn't exist yet,
it will be created on the fly.
And then on this database,
we can get access to our meetupsCollection.
Now, MongoDB is a NoSQL database
that works with collections full of documents.
Collections would be kind of your tables in a SQL database
and documents would be your entries in those tables.
So you have collections,
which hold multiple documents.
And a single meetup would be a single document,
the overall collection then holds multiple meetups,
multiple meetup documents.
And you get hold of a collection by using your database
and then the collection method.
And the collection can have any name of your choice.
Just as the database if it doesn't exist yet,
it will be generated on the fly.
And it could be named meetups as well.
So it can have the same name as the database
but you can also use different names here.
And now that we got hold of the meetupsCollection,
on that collection here, we can call insertOne,
which is one of the built-in query commands
for inserting one new document into this collection.
And now the great thing about MongoDB
is that a document is just a object in the end,
a JavaScript object.
And that now could be an object with title,
image, address and description.
And since that's the case,
since that would make a lot of sense,
we can also just directly insert data,
so this full data object into our database.
And hence, of course,
we don't even need to use destructuring here.
Now we would insert this data object into the database.
Now, this also is an async operation.
insertOne returns a promise
and hence, we can await this here as well
to get back the result of this operation.
Result will be an object
with, for example, the automatically generated ID.
We can console.log result here if we want to.
Now, we can also add error handling here
to handle the case that connecting failed
or inserting failed.
And I do this in the full course
but here, let's keep this focused and concise
and let's simply assume that it will work.
You can simply use try catch to wrap this
to add error handling if you want to.
Now, I wanna call client.close then
to close the database connection once we're done
and then we need to use this response object
to send back a response
because we're getting a request,
we're then storing data in the database,
ultimately, we also need to send back a response then.
And we do this with this response object.
Now, this works similar to what you might be used to
from Node Express.
You have a status method,
which you can call on response
to set a HTTP status code
of the response which will be returned.
For example, a 201 status code
to indicate that something was inserted successfully.
You can then chain a JSON call here
to prepare the JSON data that will be added
to the outgoing response.
And here we could, for example, add a message key
where we say Meetup inserted!
But of course, it's totally up to you
which kind of response you wanna return.
With that, however, we have a basic API route,
which will insert meetups into our database
and therefore, in the next step,
we can now send a request to this API route
from the front end when this form here is submitted
so that we actually do trigger this API route
and we use that code here.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.