MERN mongo connecting error

const express = require('express');

const cors = require('cors');

const mongoose = require('mongoose');

require('dotenv').config();

const app = express();

const port = process.env.PORT || 3000;

//middleware

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

})

const exercisesRouter = require('./routes/exercises');

const usersRouter = require('./routes/users');

app.use('/exercises', exercisesRouter);

app.use('/users', usersRouter);

app.listen(port, () => {

console.log(`Server is running on port: ${port}`);

});

and my terminal error

[nodemon] 2.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server is running on port: 3000
(node:1487) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:1487) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cosmic-shard-00-02-qytne.gcp.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cosmic-shard-00-02-qytne.gcp.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (/Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/connection.js:356:9)
    at Object.onceWrapper (events.js:300:26)
    at TLSSocket.emit (events.js:210:5)
    at net.js:658:12
    at TCP.done (_tls_wrap.js:481:7) {
  name: 'MongoNetworkError',
  errorLabels: [Array],
  [Symbol(mongoErrorContextSymbol)]: {}
}]
    at Pool.<anonymous> (/Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/topologies/server.js:433:11)
    at Pool.emit (events.js:210:5)
    at /Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/pool.js:562:14
    at /Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/pool.js:999:9
    at callback (/Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/connect.js:110:5)
    at /Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/connect.js:130:7
    at _callback (/Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/connect.js:320:5)
    at Connection.errorHandler (/Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/connect.js:336:5)
    at Object.onceWrapper (events.js:300:26)
    at Connection.emit (events.js:210:5)
    at TLSSocket.<anonymous> (/Users/macbookpro/Desktop/Mango DB CRUD/mern-exercise-tracker/backend/node_modules/mongodb/lib/core/connection/connection.js:354:12)
    at Object.onceWrapper (events.js:300:26)
    at TLSSocket.emit (events.js:210:5)
    at net.js:658:12
    at TCP.done (_tls_wrap.js:481:7)
(node:1487) 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:1487) [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.
1 Like

@dudelybhai Iā€™m facing the same problem. How did you resolve this?

Welcome, mymailinglistdiv.

Seeing as this post is over 9 months old, and the user has not been active in as long, I recommend you open a new topic for your own problem.

Remember to provide sufficient detail, and correctly format your code/errors using MarkDown.