I am trying to connect my node app to a mongoDB atlas DB. I have followed all the steps i usually follow but for some reason it just wont connect. My code is as below (it usually uses a .env file to store the connection string but i have simplified into a variable for testing purposes)
const mongoose = require('mongoose');
//Connect to Mongoose DB
const URI = "mongodb+srv://AdminUser:**PASSWORD**@cluster0.l0f31.mongodb.net/omegadb?retryWrites=true&w=majority"
console.log("attempting to connect to MongoDB");
mongoose.connect(URI,{useNewUrlParser: true, useUnifiedTopology:true})
.then((result)=>console.log("Connected to db"))
.catch((err) => console.log(err))
This is giving me the below error in the console:
attempting to connect to MongoDB
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 (C:\HTML\omega\node_modules\mongoose\lib\connection.js:824:32)
at C:\HTML\omega\node_modules\mongoose\lib\index.js:381:10
at C:\HTML\omega\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\HTML\omega\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (C:\HTML\omega\node_modules\mongoose\lib\index.js:1234:10)
at Mongoose.connect (C:\HTML\omega\node_modules\mongoose\lib\index.js:380:20)
at Object.<anonymous> (C:\HTML\omega\app.js:20:16)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'cluster0-shard-00-00.l0f31.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01.l0f31.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02.l0f31.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-4p8j1o-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
I have checked the following, none of which seem to have helped:
My user name seems to be correct
My password is correct
The database name is correct in the connection string
I have checked that the network access in mongoDB is allowing my IP as it mentions this in the error, but it is (i actually set it to allow all IPs for testing purposes and still doesnt help)
Any ideas, i have been wracking my brain on it for hours?