Raghav Herugu
Raghav Herugu

Reputation: 546

MongoDB Mongoose error when connecting to the database

I have encountered a problem when connecting to a MongoDB database using Mongoose. I am using node js and express.

here is the error:

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)

I think the main error is this: Error: queryTxt ESERVFAIL businessregistration-npeym.mongodb.net

My code:

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}!`);
});

I do not know why this is suddenly happening. If you could help, thanks!

Upvotes: 2

Views: 1306

Answers (1)

Raghav Herugu
Raghav Herugu

Reputation: 546

After some research, scrolling through stack overflow, I finally found a solution.

Credits to JOSEPH KASULE from stack overflow: Thanks!

Link to answer: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?

Scroll down to the correct answer to find it.

Turns out, I have to use an older connection method for connecting. I had to select a node version of 2.2.12 or greater:

2.2.12 or greater

and I have to use an older version of connecting:

Instead of this:

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

I had to use this:

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

when connecting to Mongo DB using mongoose.

The reason is that according to Mongo DB, SRV was not working properly due to Mongoose.

Upvotes: 2

Related Questions