问题
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.
I have tried whitelisting the IP. Also, the same code is working fine on another machine but not on my machine.
The code is:
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}`);
});
It is supposed to give output:
Here we go on port: 5000
Connection established
But I'm getting the only the first output and the error.
回答1:
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.
来源:https://stackoverflow.com/questions/62758525/getting-timeout-error-while-connecting-mongodb-atlas-with-mongoose