I have a form and when something does not happen immediately after submitting, I want to be able to report specific error messages. I use Sequelize as my ORM, and the returned promises get a little messy with all the code embedded.
For example, if we have two models for updating: User and Photo :
models.User.find({where: {name: 'bob' }}).then(function(user) { if(user) { user.updateAttributes({email: email}).then(function(user) { models.Photo.find({where: { hash: hash}}).then(function(photo) { if(photo) res.json({"message":"updated"}); else res.status(404).json({"error": "Could not find Photo"}); }).catch(err) { res.status(500).json({"error": err}); }); }).catch(function(err) { res.status(500).json({"error": err}); }); } else { res.status(404).json({"error": "Could not find user"}); } }).catch(function(err) { res.status(500).json({"error": err}); });
And if I have 10 fields in the form for updating, all the embedded code can become overbearing.
What recommendations can be given if I want to have specific error descriptions, but also more readable code? Is it possible to fix all 404 and 500 errors in one block of code, and not break them like me?
source share