Wrong connection issue

Hi guys, I need some help. Something really strange is happening.

I have a small script:

`
const mongoose = require(‘mongoose’);

const mongoURI=“”
async function connectToDatabase() {
try {
const conn = await mongoose.connect(mongoURI);
console.log(✅ MongoDB conectado: ${conn.connection.name} en ${conn.connection.host});
} catch (error) {
console.error(error)
console.error(‘:x: Error al conectar a MongoDB:’, error.message);
process.exit(1);
} finally {
await mongoose.disconnect();
}
};

connectToDatabase()`

As you can see, all it does is try to connect to an already created MongoDB database. But it doesn’t work — it says there’s no connection. I set the network access IP to 0.0.0.0, but nothing changes.

The weird part is that I have a small Nest project that just imports Mongoose and the mongoURI, and it connects to the database without any issues.

Why could it be that the pure JS script doesn’t connect, even though that’s the only difference?

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: Configure IP Access List Entries - Atlas - MongoDB Docs
at _handleConnectionErrors (/Users/humberto.lagomarsino/Documents/personal/CodeWars/marco/node_modules/mongoose/lib/connection.js:1165:11)
at NativeConnection.openUri (/Users/humberto.lagomarsino/Documents/personal/CodeWars/marco/node_modules/mongoose/lib/connection.js:1096:11)
at async connectToDatabase (/Users/humberto.lagomarsino/Documents/personal/CodeWars/marco/index.js:7:18) {
errorLabelSet: Set(0) {},
reason: TopologyDescription {
type: ‘ReplicaSetNoPrimary’,
servers: Map(3) {
ac-zyt0faf-shard-00-02.9f9k5si.mongodb.net:27017’ => [ServerDescription],
ac-zyt0faf-shard-00-00.9f9k5si.mongodb.net:27017’ => [ServerDescription],
ac-zyt0faf-shard-00-01.9f9k5si.mongodb.net:27017’ => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: ‘atlas-34c2vg-shard-0’,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}

How does your connection string look like (without your credentials)?
Can you connect with the same connection string with another tool, e.g. Mongo Compass or Mongo Shell mongosh?

Hi!

Thank you for answering!
I solved it… and it was super weird.
the problem? The version of nodejs
I was using mongoose: 8.14.3
with node version 18 it was giving that issue, I uploaded to 20 and it work just fine :S