1

Out of nowhere, I get this error.

{ MongoNetworkError: failed to connect to server [cluster0-shard-00-01-erhon.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-erhon.mongodb.net:27017 closed]
    at Pool.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at callback (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:109:5)
    at runCommand (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:129:7)
    at Connection.errorHandler (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:321:5)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at TLSSocket.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connection.js:350:12)
    at Object.onceWrapper (events.js:277:13)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:1854) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-01-erhon.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-erhon.mongodb.net:27017 closed]
    at Pool.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:189:13)
    at connect (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at callback (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:109:5)
    at runCommand (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:129:7)
    at Connection.errorHandler (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:321:5)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at TLSSocket.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connection.js:350:12)
    at Object.onceWrapper (events.js:277:13)
(node:1854) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1854) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[nodemon] clean exit - waiting for changes before restart

I am using Mongo Atlas(meaning I have mongodb installed), and here is how I connect with mongodb.

const mongoConnect = callback => {
   MongoClient
   .connect('mongodb+srv://theodosiostziomakas:password_goes_here@cluster0-erhon.mongodb.net/shop?retryWrites=true&w=majority',
   {useNewUrlParser: true}
   )
   .then(client => {
      console.log('Connected!');
      _db = client.db();
      callback();
   })
   .catch(err => {
      console.log(err);
      throw err;
   });
}

You can also download my project from this repo.

How can this error be fixed? I also looked in this topic, but couldn't find a solution.

Thanks, Theo.

Theo
  • 3,099
  • 12
  • 53
  • 94

1 Answers1

1

Such an error is usually IP issue.

First of all, You have to whitelist your IP. Even if you mention that you did whitelist your IP, there must be some external factor that influencing, changing your IP / port (reconnecting to wifi, ISP, etc.)

And to know whether the fault lies in your code alone, try connecting your MongoDB cluster through MongoDB Compass. If it connects, then it's from your code.

Also make sure you didn't use your own MongoDB username and password but username and password you created in Security -> Database Access.


Links to other questions that had faced exactly your error, which all in the end was IP issue:

Mongoose is not connecting MongoDB Atlas (the answer)

Couldn't connect to mongodb on mongodb-atlas(the answer)

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