Am trying to npm start my backend but I am having the error with connection to mongodb atlas. here is my index.js code:
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
const app = express();
app.use(bodyParser.json({limit: "30mb", extended: true}));
app.use(bodyParser.urlencoded({limit: "30mb", extended: true}));
app.use(cors());
const CONNECTION_URL = "mongodb srv://Danishbukhari:(mypassword)@cluster0.dxzhf.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
const PORT = process.env.PORT || 5000;
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true})
.then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`)))
.catch((error) => console.log(error.message) )
and my package.json be like:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon index.js"
},
"author": "Danish",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mongodb": "^4.2.2",
"mongoose": "^6.1.2",
"nodemon": "^2.0.15"
}
}
I have also whitelisted my IP adress please tell my how to resolve this issue :(
CodePudding user response:
I had this issue and Changing DNS to 8.8.8.8
worked for me.
But before this, make sure that:
- Check your connection.
- Turn off VPN.
- IP whitelist in
mongoDB
- Check your credentials in
mongoDB
Good Luck ;-) If still have issue check it out link