【发布时间】:2022-11-27 02:04:55
【问题描述】:
我的 aws lambda 函数有一个非常烦人的问题。
有时,请求会因以下错误而失败。我不知道原因,这是非常随机的,因为我认为我的请求有 95% 成功,但失败的 5% 会造成严重损害并明显阻止我的应用程序正常运行。
Runtime.UnhandledPromiseRejection: 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://docs.atlas.mongodb.com/security-whitelist/
然而在 mongodb atlas => 网络访问中,我将 ip 地址设置为 0.0.0.0 。
这是我目前的情况
const mongoose = require('mongoose');
let conn = null;
const uri = process.env.MONGO_URI;
module.exports = function connect() {
if (conn == null) {
conn = mongoose.connect(uri, {
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 10000,
useNewUrlParser: true,
keepAlive: true,
useUnifiedTopology: true
}).then(() => mongoose);
// `await`ing connection after assigning to the `conn` variable
// to avoid multiple function calls creating new connections
await conn;
}
return conn;
}
有什么建议吗?
【问题讨论】:
标签: node.js mongodb express mongoose aws-lambda