【发布时间】:2020-09-08 12:47:00
【问题描述】:
我在使用 Mongoose 连接到 MongoDB 数据库时遇到了问题。我正在使用 node js 和 express。
这是错误:
Error: queryTxt ESERVFAIL businessregistration-npeym.mongodb.net
(node:66755) UnhandledPromiseRejectionWarning: Error: queryTxt ESERVFAIL businessregistration-npeym.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19)
(node:66755) 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)
我认为主要的错误是:
Error: queryTxt ESERVFAIL businessregistration-npeym.mongodb.net
我的代码:
const express = require("express");
const app = express();
const Schema = require("./Schema");
const mongoose = require("mongoose");
const port = process.env.PORT || 4000;
mongoose.connect(
"mongodb+srv://<username>:<password>@businessregistration-npeym.mongodb.net/test?retryWrites=true&w=majority",
{ useNewUrlParser: true, useUnifiedTopology: true }
);
mongoose.connection.on("connected", function () {
console.info("Connected!\n\n");
});
mongoose.connection.on("error", function (err) {
console.error(`ERROR!!! The error is: ${err}\n\n`); // error is consoled here
});
mongoose.connection.on("disconnected", function () {
console.warn(
"The connection has been lost. This is because it got disconnected.\n\n"
);
});
app.listen(port, () => {
console.log(`Listening on port ${port}!`);
});
我不知道为什么会突然发生这种情况。如果你能帮忙,谢谢!
【问题讨论】:
标签: node.js mongodb express mongoose