连接 MongoDB Atlas 与 Mongoose 时出现超时错误

2024-02-29

我正在尝试使用 mongoose 连接到 MongoDB Atlas 上的数据库。但每次它都会给我以下错误:

(node:2327) UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-abjwg.gcp.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19)
(node:2327) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2327) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我尝试过将IP列入白名单。另外,相同的代码在另一台机器上运行良好,但在我的机器上运行不佳。

代码是:

const express = require('express');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

// Connecting to MongoDB
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true});
const connection = mongoose.connection;
connection.once('open', () => {
    console.log('Connection established');
})

app.use(express.json());

app.listen(port, () => {
    console.log(`Here we go on port: ${port}`);
});

它应该给出输出:

Here we go on port: 5000
Connection established

但我只得到第一个输出和错误。


您的计算机上 TXT 记录的 DNS 解析似乎已损坏。您可以使用旧版 URI(没有srv) 来连接。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

连接 MongoDB Atlas 与 Mongoose 时出现超时错误 的相关文章

随机推荐