Mongoose is not connecting MongoDB Atlas

前提是你 提交于 2020-01-13 07:01:10

问题


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:


回答1:


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));



回答2:


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




回答3:


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))




回答4:


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!!

Thanks so much for your help guys!



来源:https://stackoverflow.com/questions/56498216/mongoose-is-not-connecting-mongodb-atlas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!