Hey, guys, I am facing this problem.
(node:1903) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
I am using the latest version of mongoose -
------Sever.JS FIle-------
const express = require("express");
const connectDB = require("../backend/connection");
const app = express();
connectDB();
app.use(express.json({ extended: false }));
const Port = process.env.Port || 3000;
app.listen(Port, () => console.log("Server Started"));
-----connection.js File-------
const mongoose = require ('mongoose');
const URI = 'mongodb+srv://dbfahad:[email protected]/test?retryWrites=true&w=majority'
const connectDB = async () => {
mongoose.connect(URI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
console.log('DB Connected....');
}
module.exports = connectDB;
------Output Terminal-------
My DB is connected and Server also running but I face this problem -
DB Connected....
Server Started
(node:1903) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
at Timeout._onTimeout (/Users/fahadbinalam/mern-project/mern-project1/backend/node_modules/mongodb/lib/core/sdam/topology.js:897:9)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
(node:1903) 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:1903) [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 solved this issue :)
How did you solve, please share
How did you solve , please share I having same problem in my code .
Please share how to solve 馃挴
Hi, guys you need to add Promise like - mongoose.promise = Global.promise.
So my Connection.JS file is -
const mongoose = require ('mongoose');
const URI = 'mongodb+srv://dbfahad:[email protected]/test?retryWrites=true&w=majority'
mongoose.Promise = global.Promise
const connectDB = async () => {
mongoose.connect(URI, {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true})
console.log('DB Connected....');
}
module.exports = connectDB