Categories
Development

Cannot Connect to MongDB Atlas using Mongoose

I am currently starting to learn a MERN stack tutorial. I initially created a react app, added a directory called backend, installed express, cors, express, and dotenv, then created a server.js and added the some code. But after connecting to mongodb atlas, I get the error below:

(node:5828) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout.<anonymous> (C:\Users\Lenovo\Desktop\REACTion\mernapp\backend\node_modules\mongodb\lib\core\sdam\topology.js:878:9)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7)
(node:5828) 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:5828) [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.
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Server is running on port: 5000
Connection failed!
MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout.<anonymous> (C:\Users\Lenovo\Desktop\REACTion\mernapp\backend\node_modules\mongodb\lib\core\sdam\topology.js:878:9)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7) {
  name: 'MongoTimeoutError',
  reason: MongoNetworkError: connection 2 to cluster0-shard-00-01-l6mnq.gcp.mongodb.net:27017 closed
      at TLSSocket.<anonymous> (C:\Users\Lenovo\Desktop\REACTion\mernapp\backend\node_modules\mongodb\lib\core\connection\connection.js:356:9)
      at Object.onceWrapper (events.js:288:20)
      at TLSSocket.emit (events.js:200:13)
      at net.js:586:12
      at TCP.done (_tls_wrap.js:479:7) {
    name: 'MongoNetworkError',
    errorLabels: [ 'TransientTransactionError' ],
    [Symbol(mongoErrorContextSymbol)]: {}
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

Here is my code below for the server.js file. Also I copied the uri from mongodb atlas and pasted it on a .env file.

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, { useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true }
)
.then(() => {
console.log("Connected to database!");
})
.catch((error) => {
console.log("Connection failed!");
console.log(error);
});

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 in advanced!!!

2 replies on “Cannot Connect to MongDB Atlas using Mongoose”

Leave a Reply

Your email address will not be published. Required fields are marked *