is there a mongoose connect error callback

pkyeck picture pkyeck · Jul 13, 2011 · Viewed 53.9k times · Source

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

?

Answer

evilcelery picture evilcelery · Jul 13, 2011

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

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