1

i tryied to connect mongoDB with Node Js by below code and i getting the error

Error: querySrv EREFUSED _mongodb._tcp.e-commerce.xocsdrz.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:251:17) {
  errno: undefined,
  code: 'EREFUSED',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.e-commerce.xocsdrz.mongodb.net'
}

here is my Code to connect mongoDb with nodejs

const mongoose = require('mongoose');

// const dotenv =  require("dotenv")
// dotenv.config({path:`config/config.env`});
// const DB = process.env.DB;

// console.log(DB);

const connectDatabase = () => {
  mongoose
    .connect(
      'mongodb+srv://Karishma:<password>@e-commerce.xocsdrz.mongodb.net/?retryWrites=true&w=majority',
      {}
    )
    .then((data) => {
      console.log(`mongoDb connected with the server:${data.connection.host}`);
    })
    .catch((err) => {
      console.log(err);
    });
};

module.exports = connectDatabase;

enter image description here

lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
  • make sure you whitelist your IP address correctly. – Moman Raza Jan 03 '23 at 09:02
  • you can try these solution provided on [mongoDB community](https://www.mongodb.com/community/forums/t/querysrv-erefused-mongodb-tcp-cluster0-tpvpc-mongodb-net/8549/4) – Moman Raza Jan 03 '23 at 09:15
  • Does this answer your question? [How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?](https://stackoverflow.com/questions/55499175/how-to-fix-error-querysrv-erefused-when-connecting-to-mongodb-atlas) – Joe Jan 03 '23 at 10:26

1 Answers1

0

From the connection options choose driver version of "2.2.12 or later"

Noble Kuz
  • 211
  • 2
  • 4