I am trying to catch errors instead of throwing them. I attempted try/catch but realised that didn't work so im at a bit of a loose end. The two errors are mongodb related, however i think node/express errors are still thrown.
app.get( "/test/:lastName", function ( reqt, resp ) {
var driverName = reqt.params.lastName
mongoClient.connect( "mongodb://localhost/enteprise",
function( err, db ) {
if ( err ) {
console.error("1 mess " + err)
}
var database = db.db( "enteprise" )
var collection = database.collection( "driversCol" )
collection.findOne( { lastName : driverName },
function( err, res ) {
if ( err ) {
console.error("2 mess " + err)
}
resp.json( res.rate )
db.close()
})
})
})
CASES
if I curl -X GET localhost:3000/test/Owen then '43' should be returned as thats the rate.
currently if i curl -X GET localhost:3000/test/Cressey it throws a type error as its not in the database.
How do I catch errors given the above code and example?
thenandcatchinstead of the current system above of callbacks?