I am using monk on a code that looks like
var monk = require('monk')
var db = monk('localhost/mydb')
if(!db){
console.log('no connection')
}
when I run it, console logs 'no connection', but I would like to know why it is not connecting, (maybe see a stack trace' how do I do that?
Per https://github.com/Automattic/monk/pull/142 monk('localhost')
can now be used as a promise which resolves when the connection opens and rejects when it throws an error.
i.e.
let db = monk('localhost');
db.catch(function(err) {
console.log(err)
});
Hey I have been struggling with this and finally got a very beautiful solution regarding the same. So this a basic node philosophy where the callback has the first argument as the error object. Here in monk this holds good, the code that solves this problem is:
var monk = require('monk');
var db = monk('localhost/mydb', function(err, db){
if(err){
console.error("Db is not connected", err.message);
}
});
As you see a callback function is passed to the monk
object itself and that callback has parameters of our interests first being the err
object and the next being the db
object.
The err
object contains all the information about the error that occurs while establishing the connection and corresponding message.
You can go on and look at the err
and db
object they actually give a deeper insight on how monk has handled things and what object does mongo DB has returned.
Hope this solves your problem.
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