QuXiangjie / Property-management-MERN

www.estate.100jsprojects.com/
0 stars 0 forks source link

Connect to the database #6

Open QuXiangjie opened 7 months ago

QuXiangjie commented 7 months ago

we need download a package called mongoose

npm install mongoose

In the index.js file

import mongoose from 'mongoose' mongoose.connect("database URL")

Then we need to hide the database URL

create a file called .env npm install dotenv in the index.js file

import dotenv from 'dotenv' dotenv.config(); mongoose.connect(process.env.Mongo)

Also in the .gitignore file, we need to add .env

image

then we can deal with the database connection

mongoose.connect(process.env.mongodburl).then(
    ()=>{
        console.log("Connect to the database");
    }
).catch(
    (err)=>{
        console.log(err)
    }
)