Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a mongoose connect error callback

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

?

like image 985
Philipp Kyeck Avatar asked Jul 13 '11 09:07

Philipp Kyeck


People also ask

Does mongoose connect return a promise?

You don't handle a Promise with a callback: mongoose call you're callback if provided, otherwise it return the Promise.

Does mongoose close connection automatically?

You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.

Where do I call mongoose connect?

mongoose. connect('mongodb://localhost:27017/myapp'); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.

What is the difference between mongoose connect and mongoose createConnection?

connect() is use, whereas if there is multiple instance of connection mongoose. createConnection() is used. Hope someone can clarify more about this.


1 Answers

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

mongoose.connect('mongodb://localhost/dbname', function(err) {     if (err) throw err; }); 
like image 129
evilcelery Avatar answered Sep 20 '22 04:09

evilcelery