I am aware that other similar questions exist on this forum. My issue is intermittent and generally only occurs after the function has been idle for some time.
I am running an Express web server on Vercel. I have Mongoose version 7.6.13 installed from NPM.
I am using the following code in a server.js file to establish the Mongoose connection.
const express = require("express");
const mongoose = require("mongoose");
mongoose.connect("mongodb+srv://prod-database-vercel:[email protected]/ABC",{
useNewUrlParser: true,
useUnifiedTopology: true,
}).catch(e => {
throw new Error('error caught from mongoose connect', { cause: e })
});
Intermittently, usually after the function has sat idle for some time I will receive this error
Unhandled Rejection: 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://www.mongodb.com/docs/atlas/security-whitelist/
at _handleConnectionErrors (/var/task/backend/node_modules/mongoose/lib/connection.js:809:11)
at NativeConnection.openUri (/var/task/backend/node_modules/mongoose/lib/connection.js:784:11)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ABC-00-00.ABC.mongodb.net:27017' => [ServerDescription],
'ABC-shard-00-01.ABC.mongodb.net:27017' => [ServerDescription],
'ABC-shard-00-02.ABC.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-wnvhrz-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
Shutting down the server for 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/
shutting down the server for unhandle promise rejection
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
It can not be a firewall/allowed IP’s issue as the function works the majority of the time. I am running on the Mongo Atlas dedicated M10 plan and the server has little to no load.