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.