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) { ... });
?
You don't handle a Promise with a callback: mongoose call you're callback if provided, otherwise it return the Promise.
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.
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.
connect() is use, whereas if there is multiple instance of connection mongoose. createConnection() is used. Hope someone can clarify more about this.
When you connect you can pick up the error in the callback:
mongoose.connect('mongodb://localhost/dbname', function(err) { if (err) throw err; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With