繁体   English   中英

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

[英]Getting Timeout error while connecting MongoDB Atlas with Mongoose

我正在尝试使用 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}`);
});

它应该给output:

Here we go on port: 5000
Connection established

但是我得到了唯一的第一个 output 和错误。

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

我的 Internet 服务提供商阻止了连接。 我将我的 DNS 更改为 Google 的 Public DNS 并且错误不再存在。 我按照下面的链接更改了我的 DNS。
https://developers.google.com/speed/public-dns/docs/using

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM