is there a mongoose connect error callback

后端 未结 6 1397
别那么骄傲
别那么骄傲 2020-11-30 02:42

how can i set a callback for the error handling if mongoose isn\'t able to connect to my DB?

i know of

connection.on(\'open\', function () { ... });
         


        
6条回答
  •  [愿得一人]
    2020-11-30 03:19

    there many mongoose callback you can use,

    // CONNECTION EVENTS
    // When successfully connected
    mongoose.connection.on('connected', function () {  
      console.log('Mongoose default connection open to ' + dbURI);
    }); 
    
    // If the connection throws an error
    mongoose.connection.on('error',function (err) {  
      console.log('Mongoose default connection error: ' + err);
    }); 
    
    // When the connection is disconnected
    mongoose.connection.on('disconnected', function () {  
      console.log('Mongoose default connection disconnected'); 
    });
    
    // If the Node process ends, close the Mongoose connection 
    process.on('SIGINT', function() {  
      mongoose.connection.close(function () { 
        console.log('Mongoose default connection disconnected through app termination'); 
        process.exit(0); 
      }); 
    }); 

    more on: http://theholmesoffice.com/mongoose-connection-best-practice/

提交回复
热议问题