title: Cannot Connect to a MongoDB using Node.js and Mongoose
body: <p>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)</p>
  <pre><code>const mongoose = require('mongoose');
   
      //Connect to Mongoose DB  
   const URI = &quot;mongodb+srv://AdminUser:**PASSWORD**@cluster0.l0f31.mongodb.net/omegadb?retryWrites=true&amp;w=majority&quot;    
        console.log(&quot;attempting to connect to MongoDB&quot;);
        mongoose.connect(URI,{useNewUrlParser: true, useUnifiedTopology:true})
              .then((result)=&gt;console.log(&quot;Connected to db&quot;))
              .catch((err) =&gt; console.log(err))
  
  </code></pre>
  <p>This is giving me the below error in the console:</p>
  <pre><code>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 (&lt;anonymous&gt;)
      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.&lt;anonymous&gt; (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' =&gt; [ServerDescription],
        'cluster0-shard-00-01.l0f31.mongodb.net:27017' =&gt; [ServerDescription],
        'cluster0-shard-00-02.l0f31.mongodb.net:27017' =&gt; [ServerDescription]
      },
      stale: false,
      compatible: true,
      heartbeatFrequencyMS: 10000,
      localThresholdMS: 15,
      setName: 'atlas-4p8j1o-shard-0',
      maxElectionId: null,
      maxSetVersion: null,
      commonWireVersion: 0,
      logicalSessionTimeoutMinutes: null
    },
    code: undefined
  }
  </code></pre>
  <p>I have checked the following, none of which seem to have helped:</p>
  <p>My user name seems to be correct</p>
  <p>My password is correct</p>
  <p>The database name is correct in the connection string</p>
  <p>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)</p>
  <p>Any ideas, i have been wracking my brain on it for hours?</p>