我正在尝试将我的 Nodejs 应用程序连接到 MongoDB Atlas,但它抛出错误。
这是错误消息:
This is the error in the catch block: MongooseServerSelectionError: read ECONNRESET DB connection error: read 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 所需的 key 。
最佳答案
我所连接的 Wi-Fi 连接有一些策略,因此设置了一些限制,因此我收到了上述错误。
我连接到另一个 Wi-Fi 连接(没有任何策略或限制),这解决了我的问题。
干杯!
关于javascript - Mongoose服务器选择错误: read ECONNRESET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60499869/