-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Discussed in #10621
Originally posted by Nikhil22 September 28, 2023
Question
When I connect to MongoDB (via MONGODB_URI env variable, via the node driver) from my Vercel-deployed app, I get intermittent errors (MongoServerSelectionError) that present as 500 errors in my Vercel-deployed app. The errors go away on refresh.
An example of the error is:
ERROR Unhandled Promise Rejection
{
"errorType":"Runtime.UnhandledPromiseRejection",
"errorMessage":"MongoServerSelectionError: Server selection timed out after 30000 ms",
"reason":{
"errorType":"MongoServerSelectionError",
"errorMessage":"Server selection timed out after 30000 ms",
"reason":{
"type":"ReplicaSetNoPrimary",
"servers":{
},
"stale":false,
"compatible":true,
"heartbeatFrequencyMS":10000,
"localThresholdMS":15,
"setName":"atlas-j7739j-shard-0",
"maxElectionId":null,
"maxSetVersion":null,
"commonWireVersion":0,
"logicalSessionTimeoutMinutes":null
},
"stack":[
"MongoServerSelectionError: Server selection timed out after 30000 ms",
" at Timeout._onTimeout (/var/task/node_modules/mongodb/lib/sdam/topology.js:278:38)",
" at listOnTimeout (node:internal/timers:569:17)",
" at process.processTimers (node:internal/timers:512:7)"
]
},
"promise":{
},
"stack":[
"Runtime.UnhandledPromiseRejection: MongoServerSelectionError: Server selection timed out after 30000 ms",
" at process.<anonymous> (file:///var/runtime/index.mjs:1250:17)",
" at process.emit (node:events:526:35)",
" at emit (node:internal/process/promises:149:20)",
" at processPromiseRejections (node:internal/process/promises:283:27)",
" at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"
]
}
"LAMBDA_RUNTIME Failed to post handler success response. Http response code":"400.
Just to be clear, the connection string and credentials work and are correct as it does connect properly when errors are not there, but when errors that I describe above do occur, it crashes the entire app.
I've written about this in detail in the MongoDB community here (https://www.mongodb.com/community/forums/t/no-way-to-avoid-replicasetnoprimary-errors/243498).
I've also been in contact with MongoDB support and they said that there are no problems with the Mongo cluster itself.
Moreover, upon further research, it seems that this is a common problem when trying to connect to MongoDB from Vercel. For example, here (https://www.mongodb.com/community/forums/t/intermittently-getting-mongoserverselectionerror-error-on-my-next-js-vercel-deployment/242470).
I'd like to solve this problem, or at the very least, not have my entire app crash with a 500 due to the error, as it currently does.
Please help.