I am facing a simple issue that has me stumped. I am passing a parameter to an express http get function, which is then used in a query. To prevent SQL injection, I ensure that the parameter is a number.
However, due to the data structure of my client, I convert these numbers to strings. This led me to accidentally pass a string instead of a number. As a result, my application failed because invoiceId was evaluated as undefined and the query did not proceed.
To address this issue, I have implemented a null check. Below is a functional example (with some adjustments to avoid compile errors when casting a string to a number):
(It was later discovered that the value being passed was the string "undefined," causing confusion. The problem persists as TypeScript does not allow checking if invoiceId is a string value since it should be a number. I wrongly assumed is
enforced type!)
class IBadInput { value: any };
var badInput = { value: "undefined" } as IBadInput;
var invoiceId = badInput.value as number;
if (typeof invoiceId == 'undefined' || invoiceId == null)
{
console.log("invoice id not provided");
}
console.log("getting for invoice", invoiceId);
However, when a string invoiceId is provided, the statement invoiceId == null is not triggered. Here is the output:
getting for invoice undefined
I have attempted various checks like invoiceId == undefined
, typeof invoiceId == null
, and simply if(invoiceId)
to see if it's "truthy," but none of them work.
Do you have any insights on why this is happening and how I can resolve it?