【问题标题】:Getting Timeout error while connecting MongoDB Atlas with Mongoose将 MongoDB Atlas 与 Mongoose 连接时出现超时错误
【发布时间】:2020-10-26 17:21:25
【问题描述】:

我正在尝试使用 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

但我得到了唯一的第一个输出和错误。

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

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

    【讨论】:

    • 请详细说明。或者建议我一些教程或博客来做同样的事情。
    • 我不知道它为什么坏了,但这似乎是一个常见问题。也许与 systemd 有关。
    • 我将我的 DNS 更改为 Google 的 DNS。现在它正在工作。
    【解决方案2】:

    我的互联网服务提供商阻止了连接。我将我的 DNS 更改为 Google 的公共 DNS,并且不再出现错误。我按照下面的链接更改了我的 DNS。
    https://developers.google.com/speed/public-dns/docs/using

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 2023-01-18
      • 2017-06-29
      • 2023-03-27
      • 2019-03-06
      • 2020-03-08
      • 1970-01-01
      • 2020-08-31
      • 2017-12-18
      相关资源
      最近更新 更多