【发布时间】:2020-03-11 07:38:48
【问题描述】:
我正在尝试将我的 Nodejs 应用程序连接到 MongoDB Atlas,但它抛出了一个错误。
这是错误信息:
这是 catch 块中的错误:MongooseServerSelectionError: 读取 ECONNRESET 数据库连接错误:读取 ECONNRESET
以下是我的 Nodejs 应用程序的代码:
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
//db connection
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("DB Connected"))
.catch(err => {
console.log("This is the error in the catch block: " + err);
});
mongoose.connection.on("error", err => {
console.log(`DB connection error: ${err.message}`);
});
注意:-我声明了 .env 文件,其中我有一个变量 MONGO_URI 来保存连接到 MongoDB Atlas 所需的密钥。
【问题讨论】:
-
您是否在 Atlas 上将您的 IP 地址列入白名单?
-
Atlas 默认需要 SSL,您的连接字符串是否需要 SSL?
标签: javascript node.js mongodb backend mongodb-atlas