Connection problem on Render only. Error says IP not whitelisted, but it is

I get this weird problem when hosting my project to Render. Locally everything works, but when uploading on Render it gives me the following error:

Database connection error: 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://www.mongodb.com/docs/atlas/security-whitelist/
connection error:  MongoServerSelectionError: Client network socket disconnected before secure TLS connection was established
    at Topology.selectServer (/opt/render/project/src/node_modules/mongodb/lib/sdam/topology.js:320:38)
    at async Topology._connect (/opt/render/project/src/node_modules/mongodb/lib/sdam/topology.js:204:28)
    at async Topology.connect (/opt/render/project/src/node_modules/mongodb/lib/sdam/topology.js:156:13)
    at async topologyConnect (/opt/render/project/src/node_modules/mongodb/lib/mongo_client.js:233:17)
    at async MongoClient._connect (/opt/render/project/src/node_modules/mongodb/lib/mongo_client.js:246:13)
    at async MongoClient.connect (/opt/render/project/src/node_modules/mongodb/lib/mongo_client.js:171:13)
    at async NativeConnection.createClient (/opt/render/project/src/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:320:3) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-sqzzpbe-shard-00-00.c0g8edm.mongodb.net:27017' => [ServerDescription],
      'ac-sqzzpbe-shard-00-01.c0g8edm.mongodb.net:27017' => [ServerDescription],
      'ac-sqzzpbe-shard-00-02.c0g8edm.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-6fjq6i-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {},
  [cause]: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
      at TLSSocket.<anonymous> (/opt/render/project/src/node_modules/mongodb/lib/cmap/connect.js:285:44)
      at Object.onceWrapper (node:events:628:26)
      at TLSSocket.emit (node:events:513:28)
      at emitErrorNT (node:internal/streams/destroy:151:8)
      at emitErrorCloseNT (node:internal/streams/destroy:116:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
    [cause]: Error: Client network socket disconnected before secure TLS connection was established
        at connResetException (node:internal/errors:718:14)
        at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
        at TLSSocket.emit (node:events:525:35)
        at endReadableNT (node:internal/streams/readable:1359:12)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      code: 'ECONNRESET',
      path: undefined,
      host: 'ac-sqzzpbe-shard-00-00.c0g8edm.mongodb.net',
      port: 27017,
      localAddress: undefined
    }
  }
}

I did whitelist the 3 IPs Render needs though and it worked before. The update to my project includes new routes and an api route to Google Apps Script, but no changes to the mongo settings. If I revert the update it works again. Render can’t find the error on their side, I am not sure what could be wrong code wise (not saying this isn’t a possibility, I am only a hobbyist). This is how I connect to MongoDb in my app.js:

mongoose.connect(dbUrl, {  
    ssl: true,
    tls: true,
    serverSelectionTimeoutMS: 5000,
}).then(() => {
    console.log("Database connection successful!");
}).catch(err => {
    console.error("Database connection error:", err.message);
});

dbUrl: mongodb+srv://REDACTED:REDACTED@REDACTED.c0g8edm.mongodb.net/?retryWrites=true&w=majority&appName=REDACTED

Every possible solution I googled tells me to whitelist the necessary IPs, but I already did and it worked before.

npm update mongoose did fix it, not sure why I didn’t do it earlier. Still not sure why an ealier version of my project worked without the update, but I am just relieved everything works fine now.

1 Like