I have been working on a React application that would connect to MongoDB Atlas with Mongoose. Using nodemon and webpack for hot reloading it would reconnect to Mongo within milliseconds every time there was a reload. It was working nicely like this for months, then suddenly began taking 20+ seconds to connect on every reload - which has significantly increased dev time.
I cannot figure out what caused this to start happening and how to fix it. Any advice would be greatly appreciated.
Note: My IP Address has already been whitelisted.
const db = "mongodb+srv://<username>:<password>@cluster0-omitted.mongodb.net/<dbname>?retryWrites=true&w=majority"
const connectDB = async () => {
try {
console.log('Attempting to connect to MongoDB...');
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
console.log('MongoDB connected...');
} catch (err) {
console.log(err.message);
process.exit(1); // Exit process with failure
}
};