Mongojs is thin wrapper for mongodb-native library, but the only problem is that I am unable to listen for errors ?
The main errors which needs to be handled are connection erros, could not or lost connection. Here is the example, that I have tried so far.
var mongojs = require('mongojs');
var db = mongojs.connect('reallyRandomStringShouldGiveError', ['sessions', 'unknown']);
// This does not work
db.on('error', function(err) {
console.log('Catch ', err);
});
db.sessions.insert({ a: 1 });
db.unkknown.find({ a: 1}, function (err, result) {
console.log(err, result);
});
And the code from "mongojs" library which should emit the errors again mongojs source
var connect = function(config, collections) {
var connectionString = parseConfig(config);
var ondb = thunky(function(callback) {
mongodb.Db.connect(connectionString, function(err, db) {
if (err) return callback(err);
that.client = db;
db.on('error', function(err) {
process.nextTick(function() {
// Errors seems to be emitted again
that.emit('error', err);
});
});
callback(null, db);
});
});
var that = new Database(ondb);
.....
return that;
};
Adding the process listener is not the best option.
process.on('uncaughtException', function(err) {
console.log(err);
});
I think this that wasn't working for you then:
because it was an issue posted on Github that finally got solved, have a look here... so you can now catch it with any problems (I am using it that way inside some of my coding and it is working!).
You can go further, and pass an
err
parameter inside your callbacks, for example like this:...to know where that error took place. This is working really nice for me.