Getting Timeout error while connecting MongoDB Atlas with Mongoose | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Getting Timeout error while connecting MongoDB Atlas with Mongoose

I am trying to connect to my database on MongoDB Atlas using mongoose. But every time it's giving me the following error: (node:2327) UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-abjwg.gcp.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19) (node:2327) 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) (node:2327) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. I have tried whitelisting the IP. Also, the same code is working fine on another machine but not on my machine. The code is: const express = require('express'); const mongoose = require('mongoose'); require('dotenv').config(); const app = express(); const port = process.env.PORT || 5000; // Connecting to MongoDB const uri = process.env.ATLAS_URI; mongoose.connect(uri, {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}); const connection = mongoose.connection; connection.once('open', () => { console.log('Connection established'); }) app.use(express.json()); app.listen(port, () => { console.log(`Here we go on port: ${port}`); }); It is supposed to give output: Here we go on port: 5000 Connection established But I'm getting the only the first output and the error.

6th Jul 2020, 4:34 PM
Shashank Shekhar
Shashank Shekhar - avatar
1 Answer
+ 2
Use connection.on('error', function(err) { console.error('MongoDB event error: ' + err); }); To examine error.
7th Jul 2020, 1:56 AM
Calviղ
Calviղ - avatar