Mongoose failed to connect and Atlas MongoDB expect to whitelist IP while all IP has allowed help

Hi, I hope someone helps me, I’m not new to mongodb, I’m over 2 years old, everything was going great, I use mongoose with nodejs, until now all my projects that used mongodb stopped, I thought it was a problem with replit where I’m hosted everything, but then I realized that mongoose can’t make the connection and gives the following 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”, I have enabled all IPs as I have always done in these last 2 years, I have not even changed the code, the mongoose package was in the version v5 with the code to connect from that version that I have always used, try now to update it and use the updated mongoose code but it still doesn’t work, I need help please.

It is possible your connection string is changed, especially if you are on the free tier. I suggest starting to check that first.

I check it all time, i also reset the password

Then, there might be a breaking change or a breaking bug in mongoose. have you tried to downgrade to what were you using before, if you remember the settings?

Since you haven’t shared any configuration, we can only point to possible causes.

“could not connect” or “connection timed out” are mostly due to IP restriction, hostname/IP change, or driver update incompatibility. since you say you checked the first two, please focus on the mongoose version for a while.

i fork and now is working, but i will notify here if it broke again, my mongoose version is the last with the best code to connect this:

  log(chalk.green(`${settings.line}`));
  log(chalk.blue("[INFO]: Loading Mondodb...."));
  log(chalk.green(`${settings.line}`));

  try {
    const options = {
      autoIndex: false, // Don't build indexes
      maxPoolSize: 10, // Maintain up to 10 socket connections
      serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
      socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
      family: 4 // Use IPv4, skip trying IPv6
    };
    await connect(uri, options).then(() => {
      log(chalk.green(`${settings.line}`));
      log(chalk.blue("[INFO]: Ready MongoDB ✅"));
      log(chalk.green(`${settings.line}`));
    }).catch((e) => {
      console.log(e);
    })
  } catch (e) {
    console.log(e);
  }

it is nice to hear. maybe it was a temporary connection issue from replit servers.

the code is on replit ar hosting it good, it just say it cant connect to the database, and now one of them broke again.

is it private? or we may fork and try to investigate. if you haven’t done so yet, you may set up “secrets” to hold you server/password details. Can’t speak for the code itself.

I can’t give u the project because I don’t have permissions, but I can give u the db.ts file that run the mongoose code: db.ts - Pastebin.com

and i import that like this:

import db from "./DB/db";
db();

let me try it with await

import db from "./DB/db";
(async () =>{
await db();
})()

“await” might do some tricks. and you may not need to wrap depending on project type ("type":"module")

Anyways, I guess I could wrap your above code correctly. I just had to comment out loadmodules and IIFE block at the end.

took me a while to adapt missing parts and some “import” issues, but it does connect to my cluster with no problem.

It works with await, is working rn, i use typescript for that i need ("type": "module") and the loadmodules is my system to make my own functions in typescript to mongoose, thanks for help.

nice to hear it is working :+1:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.