Mongoose Connection

Hello,
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");
dotenv.config();

mongoose.connect(
    process.env.MONGO_URL,
    {useNewUrlParser:true})
    .then(()=>{
        console.log("Connected to MongoDB");
    })
    .catch(()=>{
        console.log("Couldn't connect to MongoDB");
    })

//middleware
app.use(express.json());
app.use(helmet());
app.use(morgan('common'));

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

app.listen(8800,()=>{
    console.log("Backend server is running!")
})

image

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

Hi,

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

{ useNewUrlParser: true }

So, just do this:

mongoose.connect(process.env.MONGO_URL)
  .then(()=>{
    console.log("Connected to MongoDB");
  })
  .catch(()=>{
    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.