1

This is the first time I use MongoDB Atlas to work with Mongo, and while trying to connect, that's the error I get:

Error: connect ECONNREFUSED 3.209.60.172:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14) {
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {}
}

This is my code:

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

const app = express();

mongoose.connect('mongodb+srv://johnnybox:<password>@cluster0-cgxqx.mongodb.net/test?retryWrites=true&w=majority', { 
  useNewUrlParser: true
}).then(() => console.log('MongoDB Connected...'))
  .catch(err => console.log(err));

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(require('./routes'));

app.listen(3331);

ps* I'm not missing my credentials

Already looked for a solution here but there's nothing similar to my problem.

My whitelist:

enter image description here

jvbs
  • 417
  • 2
  • 11
  • 27
  • Remove ```&w=majority``` from the end of the ```uri``` part of ```mongoose.connect()```. If it didn't work, bring your ```mongoose.connect()``` before ```app.listen()``` and after any ```app.use()```. Let me know if it works this time. – Matin Sasan Jun 07 '19 at 19:51
  • nothing changed Matin, even removing `&w=majority` and repositioning... :/ – jvbs Jun 07 '19 at 20:01
  • according to this [answer](https://stackoverflow.com/a/54956063/11330560) it all comes down to IP issue in the end (and I know you tried). Perhaps you're using VPN or some proxy that hinders it. Or perhaps it is your ISP. Also change the region of your cluster to anywhere in US. – Matin Sasan Jun 07 '19 at 20:22
  • I now realized you might be mixing up your own MongoDB username with the "database user" you made. see if you're using database user and password you made yourself. Let me know. – Matin Sasan Jun 10 '19 at 15:07
  • 1
    i found out the problem guys – jvbs Jun 11 '19 at 21:09

5 Answers5

0

try this

mongoose
  .connect(
    'mongodb+srv://{my_user}:{mypass}@johnnybox-cgxqx.mongodb.net/johnnybox?retryWrites=true&w=majority',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected...'))
  .catch(err => console.log(err));
Afrida Anzum
  • 563
  • 4
  • 19
0

Try adding your IP Address in the mongo atlas IP Whitelist. Otherwise accept every connections if you don't need secure connection.

Prince Devadoss
  • 416
  • 3
  • 6
0

A new answer to the new error:

According to this answer, which had the same exact error, that is:

'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]:

Add your current IP to whiteList following "clusters/security/whitelist" in MongoDB website.

I'm sorry, I spent at least an hour to solve this. That's all I can do.


Old answer addressing the former error (he fixed this part, but still got a new error):

If you read the error log carefully it says:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block

That means you needed to add catch() to your mongoose connection:

mongoose.connect({some code}).then({some code}).catch(err => console.log(err))

Matin Sasan
  • 1,835
  • 1
  • 13
  • 26
0

I tried to run this code at home and it worked perfectly!

So it was something here in my office, after some testing, the problem was with the connection port that was locked.

Take a look:

Error: connect ECONNREFUSED 3.209.60.172:27017

Note that it connects to the port 27017

**The Ip is random, so it changes after every requisition.

After my Sd opened this port, everything worked properly!!

enter image description here

Thanks so much for your help guys!

jvbs
  • 417
  • 2
  • 11
  • 27
0

For those of you who tried various URIs the only thing that got it working here is to Add you IP vs allow the access from anywhere. Hope that saves you some time.

CyberMessiah
  • 1,084
  • 11
  • 14