How to handle mongoose connection error in try catch block? How to handle mongoose connection error in try catch block? mongoose mongoose

How to handle mongoose connection error in try catch block?


You forgot to add await, plus you don't need to check the readyState. When the connection is failed, it will be automatically captured by the catch block of the try-catch system.So your code block will be

class App {  async dbconnect() {    try {      await mongoose.connect("mongodb://127.0.0.2/test");     } catch (error) {      console.error(error);      process.exit(1);    }  }}