skip to Main Content

I’m working on a nextjs project (https://recurwallet.vercel.app) and it connects to mongodb atlas.

It works perfectly in localhost but in Vercel deployment occasionally I get this error with next auth.

I don’t think the user session expired because sometimes I’ve tried the application after much longer and it seemed to still be active

Can’t understand what the issue could be. Pasting logs below:

[GET] /api/auth/session?nxtPnextauth=session status=500

Unhandled Rejection: 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:573:17)
at process.processTimers (node:internal/timers:514:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-6f2vd8f-shard-00-02.wo509tr.mongodb.net:27017' => [ServerDescription],
'ac-6f2vd8f-shard-00-00.wo509tr.mongodb.net:27017' => [ServerDescription],
'ac-6f2vd8f-shard-00-01.wo509tr.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-r1jdn2-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
Node.js process exited with exit status: 128. The logs above can help with debugging the issue.
Unknown application error occurred

2

Answers


  1. Chosen as BEST ANSWER

    Finally found the solution after so many trial and error. All I had to do was to add this at the end of my Mongodb URI :

    mongodb+srv://<username>:<password>@YourAppName.wo509tr.mongodb.net/**?retryWrites=true&w=majority&appName=YourAppName**

    retryWrites=true&w=majority&appName=YourAppName <- this was the part i was missing


  2. you probably did not configure whitelisting ip in mongodb. Whitelist Ip allows access to network services only to those on a pre-approved list of IP addresses.

    In production, you created a new cloud mongodb atlas and your app is connecting to that. As far as I know, when you create a new cluster in MongoDB Atlas, there are no IP addresses whitelisted by default.

    if you add 0.0.0.0/0 to the whitelist, it will allow anyone from anywhere

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search