Whenever I start my nodejs server, I get this error: “MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted.”
Although I have added my IP address and also made the option to allow access from anywhere.
Below is my code to connect to the MongoDB database.
var express = require('express');
var bodyparser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
app.use(bodyparser.json());
mongoose
.connect("mongodb+srv://<username>:<password>@cluster0.lby7zh8.mongodb.net/myDatabase?retryWrites=true&w=majority&appName=Cluster0")
.then(() => console.log( 'Database Connected' ))
.catch(err => console.log( err ));
app.listen(3003)
A fun fact is I can connect to the cluster using a MongoDB compass.
Please help me through I have tried all the way to solve using chatGPT, and Gemini and am still stuck in this problem for a week
Nirmal Tamang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.