why env file in node js is not working to connect mongodb

Solutions on MaxInterview for why env file in node js is not working to connect mongodb by the best coders in the world

showing results for - "why env file in node js is not working to connect mongodb"
Alejandro
08 Feb 2019
1    // Create .env file 
2    inside .env 
3    MONGO_URI=mongo+src://username:password@cluster0......majority
4    //
5   // Inside app.js or server.js 
6    const dotenv = require('dotenv')
7    dotenv.config()
8
9    const mongoose = require('mongoose')
10
11    mongoose.connect(process.env.MONGO_URI, {
12        useNewUrlParser: true,
13        useUnifiedTopology: true
14    });
15    const db = mongoose.connection
16    db.on('error', error => console.error(error))
17    db.once('open', MONGO_URI => console.log('Connected to Database'))
18
Jacobo
01 Jun 2020
1mongoose.connect(process.env.MONGO_URI).then(() => {
2  console.log("DB Connected");
3});
4
5mongoose.connection.on("error", err => {
6 console.log(`DB Connection Error: ${err.message}`);
7});
8