Skip to content

Instantly share code, notes, and snippets.

@suvel-the-dev
Last active August 9, 2021 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suvel-the-dev/9929b61c22c5295940c03f8ea5b9efd0 to your computer and use it in GitHub Desktop.
Save suvel-the-dev/9929b61c22c5295940c03f8ea5b9efd0 to your computer and use it in GitHub Desktop.
subroute decalration snippt
/**
* @subroute<number>
* <param_type> param
* @param {<type>} <name>
*/
router.<method>("<subroute>", async function (req, res, next) {
const <param_variable> = req.<param_type>;
const action = <paramVariable>.action;
const userId = <paramVariable>.userId;
let result = undefined;
let isInError = 0;
let isErrorMsg = "";
let isErrorCode = "";
function validateParams() {
if (!action) {
isInError = 1;
isErrorCode = "INVALID_ACTION";
}
else if (!userId) {
isInError = 1;
isErrorCode = "INVALID_USERID";
}
}
validateParams();
if (isInError == 0) {
const arguments = {
action,
userId,
}
<sqlFunctionName>(arguments)
.then(function (results) {
result = results[0];
})
.catch(function (err) {
console.log("ERROR @ <method> of <route><subroute>", err);
isErrorCode = "FILTER_ERROR";
if (err.sqlState == 41001) {
isErrorMsg = "Make sure to provide your login.";
} else {
isErrorMsg =
err.sqlState +
`: We had difficulty performaing this action. Pleae report his number to your contact.`;
}
})
.finally(function () {
const header = getHeaderNode(isErrorCode, isErrorMsg);
res.send({
header,
results: result,
});
})
}
else {
// end isError
console.log("ERROR @ <method> of <route><subroute>");
const header = getHeaderNode(isErrorCode, isErrorMsg);
res.send({
header,
results: result,
});
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment