skip to Main Content

enter image description here I need to know what’s the problem. I am not getting the message on console for establishment of Mongodb database connection.

Here is a link to the error picture. https://drive.google.com/file/d/14cdAgAjfVX6R7pXND-FbjbK_3r-A3F-J/view?usp=share_link

server.js file

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();
const port = process.env.port || 5000;

app.use(cors());
app.use(express.json());

// debugger
var uri; // Define outside
if(process.env.ATLAS_URI){
  uri = process.env.ATLAS_URI; // Assign inside the block
  }
  
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true}
);

const connection = mongoose.connection;
connection.once('open', () => {
    console.log("MongoDB database connection established successfully");
})

const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');

app.use('/exercises', exercisesRouter);
app.use('/users', usersRouter);

app.listen(port, ()=> {
    console.log(`Server is running on port: ${port}`);
});

.env file

ATLAS_URI = mongodb+srv://tripsy25:Mongo@[email protected]/?retryWrites=true&w=majority

I tried to debug the code and found that uri was coming as undefined. Do I need to convert the password in ATLAS_URI to some other format?

3

Answers


  1. The .env file must be located in the root folder of your project. And you should run the project from the root folder. Therefore:

    Example 1

    server.js
    .env
    

    Run it with node ./server.js

    Example 2

    src
     |-server.js
    .env
    

    Run it with node ./src/server.js

    Login or Signup to reply.
  2. You did one mistake in .env file

    you did

     ATLAS_URI = mongodb+srv://tripsy25:Mongo@[email protected]/?retryWrites=true&w=majority
    

    But you have to use url of atlas as an string

    ATLAS_URI = "mongodb+srv://tripsy25:Mongo@[email protected]/?retryWrites=true&w=majority"
    
    Login or Signup to reply.
  3. The .env file must be located on the root folder and you must include it in the file where ever you need it at the very top.
    and a few changings you just need to do and the things are good to go.

    require('dotenv').config();
    const express = require('express');
    const cors = require('cors');
    const mongoose = require('mongoose');
    
    mongoose.set('useNewUrlParser', true);
    mongoose.set('useFindAndModify', false);
    mongoose.set('useCreateIndex', true);
    mongoose.set('useUnifiedTopology', true);
    
    
    
    const app = express();
    const port = process.env.port || 5000;
    
    app.use(cors());
    app.use(express.json());
    
    
    
    mongoose.connect(process.env.ATLAS_URI)
      .then(() => {
        console.log("DB connection successful")
        app.listen(port, () => {
          console.log(`app listening on port ${port}`);
        })
      })
      .catch((err) => {
        console.log(err)
      })
    
    const exercisesRouter = require('./routes/exercises');
    const usersRouter = require('./routes/users');
    
    app.use('/exercises', exercisesRouter);
    app.use('/users', usersRouter);
    

    I am not getting why you just create a var for ATLAS_URI. Just keep the code simple and neat and clean.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search