I am seeing similar posts, however none are helping me solve my problem.
Following a Udemy tutorial that builds a MERN application from scratch, I got stuck on the mongoose connection.
Here is my index.js code:
const express = require("express");
const mongoose = require("mongoose");
const app = express();
app.use(express.json());
app.listen(5000, () => console.log("Server started on port 5000"));
app.use("/snippet", require("./routers/snippetRouter"));
mongoose.connect("mongodb+srv://snippetUser:_password_@
snippet-manager.sometext.mongodb.net/main?retryWrites=
true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true
}, (err) => {
if (err) return console.log("error here " + err);
console.log("Connected to MongoDB");
});
Here is the error I am getting:
Server started on port 5000
error here MongooseServerSelectionError: Could not connect to any
servers in your MongoDB Atlas cluster. One common reason is
that you're trying to access the database from an IP that isn't
whitelisted. Make sure your current IP address is on your Atlas
cluster's IP whitelist:
https://docs.atlas.mongodb.com/security-whitelist/
As stated, I am seeing similar errors relating to an IP that isn't whitelisted.
However, in my mongoDB account, it seems that my IP is already whitelisted:
In the screenshot above, the blank part is where my IP is located (right before it says "includes your current IP address").
Since my IP is listed there, does that not mean my IP is whitelisted?
If not, how do I whitelist my IP?
After a couple of days of frustration, I went into Mongo Atlas, then into Network Access and changed the setting to "allow access from anywhere" (shown as 0.0.0.0/0
below). It removed my IP address and changed it to a wildcard IP address.
This was a deviation from the tutorial I am following on Udemy, but it did work, and I can finally proceed with the rest of the course.