将MongoDB Atlas与Mongoose连接时出现超时错误 [英] Getting Timeout error while connecting MongoDB Atlas with Mongoose

查看:48
本文介绍了将MongoDB Atlas与Mongoose连接时出现超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect to my database on MongoDB Atlas using mongoose. But every time it's giving me the following error:

(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列入白名单.另外,相同的代码在另一台计算机上也可以正常工作,但在我的计算机上却无法正常工作.

I have tried whitelisting the IP. Also, the same code is working fine on another machine but not on my machine.

代码是:

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

但是我只有第一个输出和错误.

But I'm getting the only the first output and the error.

推荐答案

TXT记录的DNS解析在您的计算机上似乎已损坏.您可以改用旧版URI(不带 srv 的URI)进行连接.

DNS resolution for TXT records appears to be broken on your machine. You can use the legacy URI (the one without srv) to connect instead.

这篇关于将MongoDB Atlas与Mongoose连接时出现超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆