0

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@123@cluster0.lwpkrde.mongodb.net/?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?

Tripti Verma
  • 1
  • 1
  • 5
  • Did you try to `console.log(process.env.ATLAS_URI)`? Is the variable even caught by the application? If it is then what error do you get when you try to interact with MongoDb in your application? – Tarmo Nov 02 '22 at 10:57
  • I have pasted the link for error image above. Can you please go though it once? – Tripti Verma Nov 02 '22 at 11:14

3 Answers3

0

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

radar155
  • 1,796
  • 2
  • 11
  • 28
0

You did one mistake in .env file

you did

 ATLAS_URI = mongodb+srv://tripsy25:Mongo@123@cluster0.lwpkrde.mongodb.net/?retryWrites=true&w=majority

But you have to use url of atlas as an string

ATLAS_URI = "mongodb+srv://tripsy25:Mongo@123@cluster0.lwpkrde.mongodb.net/?retryWrites=true&w=majority"
0

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.

Ali Iqbal
  • 344
  • 1
  • 6
  • I needed to use Database user and it's password and not the mongodb account login password. That's what the problem was. It's now resolved. Thank you for your help everyone. :) – Tripti Verma Dec 16 '22 at 21:13