I’m running MongoDB Atlas on node express and I got this error when I tested with postman.
JavaScript
x
30
30
1
const express = require('express');
2
const cors = require('cors');
3
const mongoose = require('mongoose');
4
5
require('dotenv').config();
6
7
const app = express();
8
const port = process.env.PORT || 5000;
9
10
app.use(cors());
11
app.use(express.json());
12
13
const uri = process.env.ATLAS_URI;
14
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true }
15
);
16
const connection = mongoose.connection;
17
connection.once('open', () => {
18
console.log("MongoDB database connection established successfully");
19
})
20
21
const exercisesRouter = require('./routes/exercises');
22
const usersRouter = require('./routes/users');
23
24
app.use('/exercises', exercisesRouter);
25
app.use('/users', usersRouter);
26
27
app.listen(port, () => {
28
console.log(`Server is running on port: ${port}`);
29
});
30
This is my .env
, I’m guessing the problem might be here too, Kindly help:
JavaScript
1
2
1
ATLAS_URI=mongodb+srv://userone:useronepassword1234@cluster0.swye5.mongodb.net/<dbname>?retryWrites=true&w=majority
2
Advertisement
Answer
In my case, I had to go to Atlas, and reset my whitelisted IP to the correct address.
Then I restarted my local server and tried posting again on postman… And it worked!