0

ERROR IT IS SHOWING

(node:7084) UnhandledPromiseRejectionWarning: Error: querySrv ETIMEOUT _mongodb._tcp.cluster0.ki2oq.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19)
(node:7084) 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:7084) [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.

CODE USED

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

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");
})


app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

thanks

biberman
  • 5,606
  • 4
  • 11
  • 35

1 Answers1

1

The error that you are seeing is because you did not use a try ... catch ... on your mongoose.connect.

The real issue is that the remote atlas cluster can't be connected to and that must have something todo with your uri. Did you whitelist your IP address or domain in the atlas settings? If you did all that, use the exact uri given by the atlas configuration admin and do not use the useNewUrlParser etc attributes used now in your connection uri.

A full answer can be found here, including correct use of try catch and promise rejection handling.

Bojoer
  • 898
  • 9
  • 19