0

I am trying to build a backend app using mongodb atlas. I have followed the instruction but I got error like this

{ MongooseServerSelectionError: connect ECONNREFUSED 18.140.224.177:27017
at new MongooseServerSelectionError 
...
  message: 'connect ECONNREFUSED 18.140.224.177:27017',
  name: 'MongooseServerSelectionError',
  reason:
   TopologyDescription {
     type: 'ReplicaSetNoPrimary',
     setName: null,
     maxSetVersion: null,
     maxElectionId: null,
     servers:
      Map {
        'database-shard-00-01-ayo9w.mongodb.net:27017' => [ServerDescription],
        'database-shard-00-00-ayo9w.mongodb.net:27017' => [ServerDescription],
        'database-shard-00-02-ayo9w.mongodb.net:27017' => [ServerDescription] 
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: null,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: null },
  [Symbol(mongoErrorContextSymbol)]: {} }

and here is my code

const mongoose = require('mongoose');
mongoose.connect("mongodb+srv://thanh:Abcdefghijk@database-ayo9w.mongodb.net/test?retryWrites=true&w=majority",
    { useNewUrlParser: true, useUnifiedTopology: true, },
  ).then(() => console.log('Connected to database')).
  catch(err => console.log('Error in connection', err));

My whitelist entry in mongodb atlas is 0.0.0.0/0

3 Answers3

0

Change username and password with your database username and password. Check mongoDB Atlas where you have created your database and collections.

0

The reason for this is the internet connection. I just change my wifi connection and it works. This is the link where I get the answer

Error at connecting to MongoDb Atlas Server

0

Your network or hosting has port 27017 blocked, that's why you can't access server.

  • To test if the port is blocked you can try this on terminal on mac/linux (curl portquiz.net:27017) or on cmd (nc -v portquiz.net 27017)

This should solve your issue.