0

I've tried connecting a MongoDB cloud atlas using URL in my Node application,but getting the following error:- UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-coypu.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19)

Ive connected using:-

mongoose.connect(process.env.MONGODB_URI || config.connectionString, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true  });
mongoose.Promise = global.Promise;

In order to check whether the connection is established or not ive donw using:-

mongoose.connect('config.connectionString',{
  useCreateIndex: true, 
  useNewUrlParser: true,
  useUnifiedTopology: true
}).then(
  () => {
    console.log("Database connection established!");
  },
  err => {
    console.log("Error connecting Database instance due to: ", err);
  }
);

where config.connectionString contains the URL generated in my atlas.

1 Answers1

0
mongoose
  .connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    log.info("successfully connected to db");
  })
  .catch((err) => {
    log.error("db connection failed " + err);
  });

Just make sure the URI is in correct format

Nayan
  • 638
  • 6
  • 15
  • I was using the uri directly which i got from the atlas. – rocker-hacker Apr 29 '20 at 16:38
  • please check [this](https://stackoverflow.com/questions/60696749/connect-nodejs-to-mongodb-droplet/60696974#60696974) – Nayan Apr 29 '20 at 16:40
  • Stil not able to get after changing the URI format. After changing the URI format getting that the server could not be found – rocker-hacker Apr 29 '20 at 17:26
  • Can you share the uri format and not the actual uri – Nayan Apr 29 '20 at 17:31
  • Actual one which I got from the MongoDB atlas:- ```mongodb+srv://:@/?retryWrites=true&w=majority&readPreference=secondary&ssl=true``` And the one which I've changed from your suggestion:- ``` mongodb://:@:27017/?ssl=true&replicaSet=&authSource=admin&retryWrites=true&w=majority ``` – rocker-hacker Apr 29 '20 at 17:38
  • `mongodb://:@host1:27017,host2:27017,host3:27017/test?ssl=true&replicaSet=host&authSource=admin&retryWrites=true&w=majority` you can see 3 different hosts on atlas, there is a fallback logic for connection. kindly try this – Nayan Apr 29 '20 at 18:13
  • Another thing is in the network setting of atlas allow the IP you are connecting from. – Nayan Apr 29 '20 at 18:13
  • Yes,I had whitelisted all ip addr by specifying 0.0.0.0/0 – rocker-hacker Apr 29 '20 at 18:25