I am trying to connect to the express server in which I was able to connect earlier but now am no longer able to connect getting 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. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (D:\development_work\study\MERN_stack\mern_project\practical\travelogger\server\node_modules\mongoose\lib\connection.js:807:32) at D:\development_work\study\MERN_stack\mern_project\practical\travelogger\server\node_modules\mongoose\lib\index.js:342:10 at D:\development_work\study\MERN_stack\mern_project\practical\travelogger\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5 at new Promise (<anonymous>) at promiseOrCallback (D:\development_work\study\MERN_stack\mern_project\practical\travelogger\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (D:\development_work\study\MERN_stack\mern_project\practical\travelogger\server\node_modules\mongoose\lib\index.js:1181:10) at Mongoose.connect (D:\development_work\study\MERN_stack\mern_project\practical\travelogger\server\node_modules\mongoose\lib\index.js:341:20) at file:///D:/development_work/study/MERN_stack/mern_project/practical/travelogger/server/index.js:22:3 at ModuleJob.run (node:internal/modules/esm/module_job:185:25) at async Promise.all (index 0) { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { 'cluster0-shard-00-00.ksifo.mongodb.net:27017' => [ServerDescription], 'cluster0-shard-00-01.ksifo.mongodb.net:27017' => [ServerDescription], 'cluster0-shard-00-02.ksifo.mongodb.net:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'atlas-w2uj2r-shard-0', logicalSessionTimeoutMinutes: undefined }, code: undefined }
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import morgan from "morgan";
import userRouter from "./routes/user.js"
const app = express();
app.use(morgan('dev'));
app.use(express.json({limit: "30mb", extended: true}));
app.use(express.urlencoded({limit: "30mb", extended: true}));
app.use(cors());
app.use("/users",userRouter); //http://localhost:5000/users/signup
const MONGODB_URL = "mongodb srv://alish:<removedThePassword>@cluster0.ksifo.mongodb.net/tour_db?retryWrites=true&w=majority"
const port = 5000;
mongoose
.connect(MONGODB_URL)
.then(() => {
app.listen(port, () => {
console.log(`server running on port ${port}`)
})
}).catch(error => {
console.log(`${port} did not connect`)
})
CodePudding user response:
Got the issue it was written in the few first line of the error message, that is "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. Make sure your current IP address is on your Atlas cluster's IP" So, I went back to my mongodb database and re-entered current ip address and problem was resolved.