0

I'm trying to connect my MERN application to MongoDB using Mongoose. the code for the same is:

const mongoose = require('mongoose');

mongoose.set('useUnifiedTopology', true);

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once('open', () => {
    console.log("MongoDB database connection established successfully");
});

But when compiled error

(node:2564) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout._onTimeout (E:\Projects\MERN Stack Projects\mern-app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:309:9)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)
(node:2564) 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(). (rejection id: 1)
(node:2564) [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.

  • Can you add this code connection.on('error', console.error.bind(console, 'connection error:')); to check exactly what is the error. It seems that there is a Promise which was not handled properly. – Prabhjot Singh Kainth Jan 02 '20 at 06:30
  • 1. Try to rerun the application may be network issue or env URL not valid. 2. Use the try-catch block to handle the error and print proper error. – Ashish Jan 02 '20 at 06:34
  • 3
    Does this answer your question? [MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms](https://stackoverflow.com/questions/59162342/mongodb-connection-error-mongotimeouterror-server-selection-timed-out-after-30) – whoami - fakeFaceTrueSoul Jan 02 '20 at 06:48

0 Answers0