I am using expressjs and mongoskin to connect to the database and I have issues when throwing an expressjs error inside my mongskin call. I can't seam to trace down the issue as the error is so dang general.
My code:
db.collection('users').find(data, {limit:1}).toArray(function(err, result) {
if(result) throw new noData('No results found');
});
Error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error
at /home/jmoney/workspace/blog/app.js:238:15
at /home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/cursor.js:129:9
at /home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/cursor.js:171:11
at /home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/cursor.js:452:35
at Cursor.close (/home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/cursor.js:660:5)
at Cursor.nextObject (/home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/cursor.js:452:17)
at [object Object].<anonymous> (/home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/cursor.js:440:12)
at [object Object].g (events.js:156:14)
at [object Object].emit (events.js:88:20)
at Db._callHandler (/home/jmoney/workspace/blog/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/db.js:1274:25)
Any ideas on why this might be breaking? or how get more in-depth error reporting? Thanks guys!
Never throw in a node.js function call unless you know what you are doing. There is no "stack" for the throw to unroll as it happens inside toArray. This leads to the exception becoming an unhandled exception.
The only way to trap that is using
http://nodejs.org/api/process.html#process_event_uncaughtexception
log it to you logfile or or use console.log()/dir() or something similar.