I am getting below error randomly. Here is my stack
- App deployed on vercel is talking to Atlas mongodb (free tier)
- Nodejs - 18.16.1
- nextjs 13
- mongodb package version - 5.8.1
ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoServerSelectionError: connection timed out","reason":{"errorType":"MongoServerSelectionError","errorMessage":"connection timed out","reason":{"type":"ReplicaSetNoPrimary","servers":{},"stale":false,"compatible":true,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"setName":"Cluster0-shard-0","maxElectionId":null,"maxSetVersion":null,"commonWireVersion":0,"logicalSessionTimeoutMinutes":null},"stack":["MongoServerSelectionError: connection timed out"," 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: connection timed out"," at process.<anonymous> (file:///var/runtime/index.mjs:1186:17)"," at process.emit (node:events:525: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)"]}
[ERROR] [1692852863980] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 400.
RequestId: 971d9da3-a51d-4511-a561-22d5702be848 Error: Runtime exited with error: exit status 128
当出现这一错误时,服务器用500个错误代码对。
谁能告诉我为什么会发生这种情况? 这是否归因于Atlas Mongodb的自由等级?
Below code shows how I am connecting to mongodb. Clientpromise is being imported and then used to do db operations in other files.
import { MongoClient } from "mongodb";
//mongodb+srv://sjhfjhfjhf/ongodb.net/awesomedb?serverSelectionTimeoutMS=60000
const uri = process.env.MONGODB_URI;
const options = {
//useUnifiedTopology: true,
useNewUrlParser: true,
serverSelectionTimeoutMS: 59000,
};
let client;
let clientPromise;
if (!process.env.MONGODB_URI) {
throw new Error("Please add your Mongo URI to .env.local");
}
if (process.env.NODE_ENV === "development") {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options);
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
// In production mode, it s best to not use a global variable.
client = new MongoClient(uri, options);
clientPromise = client.connect();
}
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise;
在这里,我是如何问答。
import { ObjectId } from mongodb ;
import clientPromise from ../../lib/mongodb
import { connectToDatabase, slugify } from ./blog ;
export async function saveAdToMongoDB(adObj) {
try {
const collection = (await clientPromise).db().collection("ads");
let catslug = slugify(adObj.businessCategory)
let slug = slugify( adObj.businessName + "-" + adObj.city + "-" + catslug)
adObj.slug = slug ;
adObj.catslug = catslug;
let advertisementObject = {...adObj, status:"pending",image:""}
let result = await collection.insertOne(advertisementObject);
} catch (err) {
console.error("Failed to save ad to MongoDB:", err);
}
}
我升级了青ongo素包和 next子包,但没有帮助。 互联网上的其他解决办法大多涉及防火墙和白化。 但我认为这不会引起这一问题。