node.js - loopback PersistedModel find() error handling -
i'm following loopback framework tutorial, page https://docs.strongloop.com/display/public/lb/extend+your+api there example code finds instance id, modified bit handle non-existent instances
coffeeshop.getname=function(id, cb) { coffeeshop.findbyid(id, function(err, shop){ if(err) { console.log(err); cb(err); } else cb(null, 'name of coffee shop '+shop.name); }); };
it works fine when call existing id, when enter invalid id, instead of calling err
handler it's omitted entirely, else
statement called , whole app crashes following console error
/app/path/here/node_modules/mysql/lib/protocol/parser.js:78 throw err; // rethrow non-mysql errors
i'm new node.js , i'm pretty sure i'm missing here shouldn't error passed callback function rather being thrown top level?
this doesn't seem mysql backend specific, switched mongo connector , got similar problem. how handle error properly?
you'll need check shop
parameter also. complete check this:
coffeeshop.getname=function(id, cb) { coffeeshop.findbyid(id, function(err, shop){ if(err) { console.log(err); cb(err); } else { if(shop) { cb(null, 'name of coffee shop '+shop.name); } else { var error = new error(); error.message = 'coffee shop not found.'; error.statuscode = 404; cb(error); } } }); };
here pattern loopback uses built-in models. code file node_modules/loopback/common/models/user.js
, @ around 281 line number.
user.logout = function(tokenid, fn) { fn = fn || utils.createpromisecallback(); this.relations.accesstokens.modelto.findbyid(tokenid, function(err, accesstoken) { if (err) { fn(err); } else if (accesstoken) { accesstoken.destroy(fn); } else { fn(new error('could not find accesstoken')); } }); return fn.promise;
Comments
Post a Comment