Mongoose Connection

I have a problem trying to connect to mongoDb ( using mongoose). It all worked untill today but nothing is changed.
First i thought that there is a problem with my code, but after checking the connection it shows that the problem is there.

This is the index.js:

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");
const userRoute = require("./routes/users");
const authRoute = require("./routes/auth");
const postRoute = require("./routes/posts");

        console.log("Connected to MongoDB");
        console.log("Couldn't connect to MongoDB");


app.use("/api/auth", authRoute);
app.use("/api/users", userRoute);
app.use("/api/posts", postRoute);

    console.log("Backend server is running!")


1 Like

instead of your message, can you print the “actual” error coming from mongoose?

it is possible you have IP security on your cluster and also your IP has changed. if you do not have a static IP on your network, your ISP may change your IP when your router restarts causing this kind of problem. please check this possibility first. open your cluster security and add your current IP to the list and see if it fixes the problem. if so, also remove the old IP entry.

    .catch( (e)=> console.log(e) )
1 Like


If you are using Mongoose v6+, then you should remove the following config:

{ useNewUrlParser: true }

So, just do this:

    console.log("Connected to MongoDB");
    console.log("Couldn't connect to MongoDB");

More info

1 Like

Thx man, u saved me. My ip wasn’t listed in the whitelist.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.