is there a mongoose connect error callback

JavascriptMongodbnode.jsMongoose

Javascript Problem Overview


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 () { ... });

but is there something like

connection.on('error', function (err) { ... });

?

Javascript Solutions


Solution 1 - Javascript

When you connect you can pick up the error in the callback:

mongoose.connect('mongodb://localhost/dbname', function(err) {
    if (err) throw err;
});

Solution 2 - Javascript

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/

Solution 3 - Javascript

In case anyone happens upon this, the version of Mongoose I'm running (3.4) works as stated in the question. So the following can return an error.

connection.on('error', function (err) { ... });

Solution 4 - Javascript

As we can see on the mongoose documentation for Error Handling, since the connect() method returns a Promise, the promise catch is the option to use with a mongoose connection.

So, to handle initial connection errors, you should use .catch() or try/catch with async/await.

In this way, we have two options:

Using the .catch() method:

mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true })
.catch(error => console.error(error));

or using try/catch:

try {
    await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
} catch (error) {
    console.error(error);
}

IMHO, I think that using catch is a cleaner way.

Solution 5 - Javascript

Late answer, but if you want to keep the server running you can use this:

mongoose.connect('mongodb://localhost/dbname',function(err) {
    if (err)
        return console.error(err);
});

Solution 6 - Javascript

  • Handle (catch) the connect exceptions
  • Handle other connection errors
  • show a message when successfully connected
mongoose.connect(
  "mongodb://..."
).catch((e) => {
  console.log("error connecting to mongoose!");
});
mongoose.connection.on("error", (e) => {
  console.log("mongo connect error!");
});
mongoose.connection.on("connected", () => {
  console.log("connected to mongo");
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilipp KyeckView Question on Stackoverflow
Solution 1 - JavascriptevilceleryView Answer on Stackoverflow
Solution 2 - JavascriptthisarattrView Answer on Stackoverflow
Solution 3 - JavascriptAstaView Answer on Stackoverflow
Solution 4 - JavascriptvaldeciView Answer on Stackoverflow
Solution 5 - JavascriptvcanalesView Answer on Stackoverflow
Solution 6 - JavascriptyayaView Answer on Stackoverflow