I have created a database named priceDatabase in mongodb atlas. Also I have created a user with usename 'admin' and password 'admin'. I am trying to connect to my node js application to the mongodb atlas. For this I am using mongoose.
const mongoose = require("mongoose");const dotenv = require("dotenv");dotenv.config();mongoose .connect(process.env.MONGO_URI) .then((db) => { console.log("Connected to the mongodb server successfuly: "); }) .catch((err) => console.log(err));
.env file
MONGO_URI = mongodb+srv://admin:admin@pricedatabase.re29j.mongodb.net/priceDatabase?retryWrites=true&w=majority
Now if I run the code I am getting error as
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 (B:\projects\priceTagServer\node_modules\mongoose\lib\connection.js:797:32) at B:\projects\priceTagServer\node_modules\mongoose\lib\index.js:341:10 at B:\projects\priceTagServer\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5 at new Promise (<anonymous>) at promiseOrCallback (B:\projects\priceTagServer\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (B:\projects\priceTagServer\node_modules\mongoose\lib\index.js:1167:10) at Mongoose.connect (B:\projects\priceTagServer\node_modules\mongoose\lib\index.js:340:20) at Object.<anonymous> (B:\projects\priceTagServer\models\db.js:10:4) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) {'pricedatabase-shard-00-01.re29j.mongodb.net:27017' => [ServerDescription],'pricedatabase-shard-00-02.re29j.mongodb.net:27017' => [ServerDescription],'pricedatabase-shard-00-00.re29j.mongodb.net:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'atlas-bew5c1-shard-0', logicalSessionTimeoutMinutes: undefined }}
It is saying my is not whitelisted in the mongodb atlas but I have given network access to 0.0.0.0/0 which means every device can have access to the database. Then, why I am still getting that error? Am I missing something?