我正在开发一个 nextjs 项目(https://recurwallet.vercel.app),它连接到 mongodb atlas。
它在 localhost 中运行良好,但在 Vercel 部署中偶尔我会在下次身份验证时收到此错误。
我不认为用户会话已过期,因为有时我会在很长时间后尝试该应用程序,但它似乎仍然处于活动状态
无法理解问题所在。粘贴日志如下:
[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
经过多次尝试和错误终于找到了解决方案。我所要做的就是将其添加到我的 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