Skip to content

Instantly share code, notes, and snippets.

@paulvstheworld
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save paulvstheworld/9105613 to your computer and use it in GitHub Desktop.

Select an option

Save paulvstheworld/9105613 to your computer and use it in GitHub Desktop.
// example of response with errors
var respObjWithErrors = {
errors: [{
key: 'could.not.insert',
msg: "couldn't insert new usr into db"
}],
data: { /* some data goes here */}
};
var respObj = { errors:[], data:{/* some data goes here */} }
someAlreadyDefinedCallback(respObj);
/* some controller function that gets passed as
a callback to a database query function */
var someControllerFunction = function(respObj) {
var errorMessagesArr = [];
if (respObj.errors.length > 0){
for(var i=0; i<respObj.errors.length; i++) {
errorMessagesArr.push(respObj.errors[i].msg);
}
resp.send(errorMessagesArr.join("\n"));
}
else {
resp.send(respObj.data);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment