I am using mongoskin to connect to mongodb from node but an exception is thrown when connecting if mongodb is not available and I can't seem to find a way to handle this.
My connection and query code look like this...
var mongo = require('mongoskin');
var conn = 'localhost:27017/dbname';
mongo.db(conn).collection('collection').find({ date: { $gte: now } }, { sort: [['date', 1]]}).toArray(function(err, result) {
if (err) throw err;
callback(null, result)
});
How should I deal with this?
Thanks, Simon
throw err;
is useless here - there is no outer scope to catch the error since it is being called by the event loop as an asynchronous function.
The convention in node.js is to fire a callback where the first arg contains an error (or null, if there is none), and the second arg contains the command result.
So you probably just want:
callback(err, result)
Or even just pass callback directly. In the code for your callback, check if err is null, and then handle the condition there.