Okay, so now we want to be able to connect to our database through our application so that we can make
queries.
And we're going to use a tool called Mongoose to do that, which is an object data mapper.
Okay.
So we can connect to it.
As you can see here, we bring it in, we call Connect, we put in our string and then from there we
can create what are called models.
Okay?
We create a schema, which is basically the fields that you want for each model.
So for example, we're going to have a products model and we'll have a schema that'll have fields like
the product name, the price, the description and so on.
Okay.
Basically everything that we have in that in the JavaScript file that we're currently fetching from.
So we want to install this in our back end dependencies.
So and you can read more about the documentation if you want, but we're going to go into the route.
I'm just going to stop the server, make sure you're in the route and let's say NPM install mongoose.
Now what we want to do is create a config file in the back end to connect to our database.
So I'm going to create a folder in back end called config.
And then inside config let's create a file called DB dot JS and then this is where we want to connect
to our database from.
So we're going to import Mongoose and then we're going to have a function called connect.
DB and we're going to make it a synchronous.
I'm going to try not to use copilot so much here, but it's going to be a synchronous because any any
methods that we call, whether it's from a mongoose model or from mongoose itself, it's going to return
a promise.
So you can either do the dot catch syntax or you can do a async await, which is what we're doing.
So we don't need to pass in any arguments or anything but in the body.
Let's do a try catch.
And in the try we're going to create a variable called con and set that to await Mongoose dot connect.
Now, we don't need a second object here or anything.
Yeah.
So just like that.
Now what's happening is we're calling Mongoose Connect, which returns a promise.
So we're awaiting on it and then we're accessing the Mongo Uri from our environment variable.
All right.
And then after that, we'll just do a console log and let's put in some backticks.
We'll say MongoDB connected and we'll put the host as well.
All right.
So if everything goes okay and we're connected, we should see this.
If there's an error, then we're just going to do a console dot.
We'll do console dot log and excuse me, let's pass in some backticks and we'll say error and then we
can just get the error message.
And then I want to exit the process with error.
So we're going to say process dot exit and pass in a one and then we just need to export default connect
DB.
So basically we just need to call this at the beginning of our application to connect.
So let's go back into Server.js and we need to bring in the file.
So I'm going to do that.
Um, I'll just do that right here.
So let's say import.
Connect.
DB And that's going to be from config db, JS and then we just need to call that.
So I'm going to put it, see, I'm going to call it.
Let's go.
Right, right, right above where we initialize the app.
So we're just going to say Connect DB.
All right, So let's save that.
And now I'm just going to run the server to test it out.
So NPM run server.
And there we go.
So we get MongoDB connected and it gives us the host.
Now, if you see something else, if you see an error, make sure you read it.
And it's probably going to have to do with your connection string.
So this right here, maybe your password or username is wrong or the name of your database, or if you
go into the Atlas dashboard, you might, you might not have added your IP address correctly because
only the IP addresses that you add there can connect to the database.
All right.
But you can also select for anywhere, you know, anywhere be able to connect as well.
So just, just double check that stuff.
So now that we're connected in the next video, what we're going to do is start to model our data because
before we can actually fetch and display the products from the database or import them or whatever we're
going to do, we need to model our data.
So we'll do that next.