簡體   English   中英

MongoDB Mongoose 連接數據庫時出錯

[英]MongoDB Mongoose error when connecting to the database

使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 連接到 MongoDB 數據庫時遇到問題。 我正在使用節點 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}!`);
});

我不知道為什么會突然發生這種情況。 如果你能幫忙,謝謝!

經過一番研究,滾動堆棧溢出,我終於找到了解決方案。

來自堆棧溢出的 JOSEPH KASULE 學分:謝謝!

答案鏈接: 連接到 MongoDB Atlas 時如何修復“錯誤:querySrv EREFUSED”?

向下滾動到正確答案以找到它。

事實證明,我必須使用較舊的連接方法進行連接。 我必須 select 2.2.12 或更高版本的節點版本:

2.2.12 或更高版本

我必須使用舊版本的連接:

而不是這個:

mongodb+srv://<username>:<password>@businessregistration-npeym.mongodb.net/test?retryWrites=true&w=majority

我不得不使用這個:

mongodb://<username>:<password>@businessregistration-shard-00-00-npeym.mongodb.net:27017,businessregistration-shard-00-01-npeym.mongodb.net:27017,businessregistration-shard-00-02-npeym.mongodb.net:27017/test?ssl=true&replicaSet=BusinessRegistration-shard-0&authSource=admin&retryWrites=true&w=majority

使用 mongoose 連接到 Mongo DB 時。

原因是根據 Mongo DB,由於 Mongoose,SRV 無法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM