Danial
Danial

Reputation: 612

mongodb + mongoose MongooseServerSelectionError

I'm trying to connect my first mongo-db application to server :

const express = require('express');
const mongoose = require('mongoose');

const app = express();

const URI = "";

mongoose
    .connect(URI,{useUnifiedTopology:true,useNewUrlParser:true})
    .then((res)=>{
        console.log(res);
        console.log(res.data);
    })
    .catch((err)=>{
        console.log("Encountered error...");
        console.log("error : "+err);
    });

const port = process.env.PORT || 5000;
app.listen(port);

console.log('App is listening on port ' + port);

But it gives me error : MongooseServerSelectionError: connection to 35.223.140.90:27017 closed.

I have white listed my IP address.

Upvotes: 1

Views: 607

Answers (3)

joshwa jutt
joshwa jutt

Reputation: 1

you can delete all the old IP address and add a new one

Upvotes: 0

Harshal Yeole
Harshal Yeole

Reputation: 4983

You need to whitelist the IP address on ATLAS

  1. Go to Network Access Panel
  2. Click Add IP Address
  3. add 0.0.0.0/0 (Allows all ips)
  4. save

This is for development only, for prod, you can add your server ip.

Upvotes: 2

Samim Hakimi
Samim Hakimi

Reputation: 725

Did you have this line in your app.js app.use(cors())?

Try to delete this line and delete everything that involves cors.

If this don't work, check your IP Whitelist inside Network Access in MongoDB Atlas and add your current IP or write this 0.0.0.0/0 to allow all IPs.

Upvotes: 3

Related Questions