【发布时间】: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