Hey Everyone. This is my Frist question on stack overflow .
I am tried to Connect my application with Mongodb. Even if I connect successfully to the server , the Mongodb connection is unsuccessful. I also set the Mongodb connection URL correctly.
Here I changed the mongoose version and Ip addresses . But it didn't work.
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app =express();
require("dotenv").config();
const PORT = process.env.PORT || 8070;
app.use(cors());
app.use(bodyParser.json());
const URL = process.env.MONGODB_URL;
mongoose.connect(URL,{
useCreateIndex:true,
useNewUrlParser:true,
useUnifiedTopology:true,
useFindAndModify:false,
});
const connection = mongoose.connection;
connection.once("open",() =>{
console.log("mongodb connection succes!");
})
app.listen(PORT,() =>{
console.log(`server is running on port ${PORT}`);
})
In the error, it is mentioned that you have not whitelisted your IP address to Atlas cluster. You need to do that. You can see here how to add your current IP address on your Atlas cluster's IP whitelist.
If this also don't work, then try changing your internet connection. Many times router block connection to 27017 port. So shifting to your mobile hotspot or other connection would work.
You need to whitelist your public IP address or else make the connection open to the world 0.0.0.0/0.
I have faced the same problem some days earlier. I don't know why it happened but I can give you a hack. You need to whitelist your IP address and also allow for anywhere both to your atlas cluster. This may fix the error as it fixed mine.