X
X
xutegino2017-09-08 17:33:18
MongoDB
xutegino, 2017-09-08 17:33:18

How to return a response to the client if a connection to Mongo is not established?

Is there a way to send a message in JSON to the client (instead of outputting to the console) when a connection to Mongo is not established (for example, it is simply not running or has crashed)?
I tried several options, this is the last one:

mongoose.connect(process.env.DATABASE, { useMongoClient: true, autoReconnect: true, reconnectInterval: 1000 }, )
  .then(() => {
    console.log('Connected to Server successfully!')
  })
  .catch((err) => {
    console.log('Unable to connect to the server. Please start the server. Error:', err.message)
    return err
  })

How to handle the error further? How to bring it to the res.jsonuser?

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