StelKizi
StelKizi

Reputation: 90

Fail to connect mongoose to Atlas-MogoDB

I have whitelisted my IP, and I have correctly replaced my username and password (including the special characters in the password) in the URI string, but it doesn't work. I also tried to allow access from everywhere, but the result was the same. I'm getting this error:

Server running on port 5000
MongooseError [MongooseServerSelectionError]: Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.
    at new MongooseServerSelectionError (C:\Users\Stella\Desktop\apps\mern-auth\node_modules\mongoose\lib\error\serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (C:\Users\Stella\Desktop\apps\mern-auth\node_modules\mongoose\lib\connection.js:823:32)
    at Mongoose.connect (C:\Users\Stella\Desktop\apps\mern-auth\node_modules\mongoose\lib\index.js:333:15)
    at Object.<anonymous> (C:\Users\Stella\Desktop\apps\mern-auth\server.js:11:10)
    at Module._compile (internal/modules/cjs/loader.js:1139:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1159:10)
    at Module.load (internal/modules/cjs/loader.js:988:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  message: "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.",
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(3) {
      'mern-stuff-shard-00-02-1tqpi.mongodb.net:27017' => [ServerDescription],
      'mern-stuff-shard-00-00-1tqpi.mongodb.net:27017' => [ServerDescription],
      'mern-stuff-shard-00-01-1tqpi.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

This is my code:

require("dotenv").config();
const express = require('express');
const mongoose = require('mongoose');

const port =  5000;
const ATLAS_URI = process.env.ATLAS_URI;
const app = express();

app.use(express.json());

mongoose.connect(ATLAS_URI, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
   })
  .then(() => console.log('mongoDB connected'))
  .catch(err => console.log(err));

  app.listen(port, () => console.log(`Server running on port ${port}`));

I've read posts, I've watched videos and spent several hours but I can't resolve this issue. Any ideas?

Upvotes: 0

Views: 268

Answers (2)

StelKizi
StelKizi

Reputation: 90

It turns out my password was causing the issue. I chose one with some special characters, and even though I followed the instructions in the documentation on how to encode them, somehow it didn't work. I terminated the cluster, created one with a password that didn't contain special characters and it worked.

Upvotes: 1

Daniel Reed
Daniel Reed

Reputation: 15

Check that your connection credentials string isn't badly formatted so it contains HTML codes instead of just text i.e. %24.

Here is an example I used a while back. Also I had an error where it can't find the test db so change that in the URI too.

mongodb+srv://USERNAME:[email protected]/DATABASE_NAME?retryWrites=true&w=majority

Upvotes: 0

Related Questions