Q
Q
qunaxis2017-02-03 16:23:17
MongoDB
qunaxis, 2017-02-03 16:23:17

How to properly catch connection errors to mongoDB through mongoose?

Right now I'm trying to handle errors like this:

export function connection() {
    mongoose.connect(`mongodb://${config.db.host}:${config.db.port}/${config.db.name}`, (err) => {
        err ? console.error(error) : console.log('Server has been connected to MongoDB');
    });
}

This is a piece of the db.js file, in the main app.js file, the db.connection() function is called.
In case of a connection error (for example, I often forget to start a database on my home server), the entire application crashes. How to catch errors and loop the connection to the database for a certain number of attempts?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question