Mongoose is not connecting MongoDB Atlas

只谈情不闲聊 提交于 2019-12-06 15:56:54

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

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

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

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!

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