I have the following problem:
const barcodeValue = element.dataset.barcode;
// the above value is sent as an argument to be verified by the function below..
if(!barcodeValue){
// run code
}
now dataset.barcode
comes from the database... currently in the schema the field barcode does not exist. So I don't know what value comes into the barcodeValue
but I assumed it to be undefined
and console.logging it confirmed this..
however if I add the barcode field in the database and keep it empty, the barcodeValue is treated as falsy and it works fine.. so the value of an empty field is falsy but the value of non-existent field (even though it shows as undefined
) is not falsy..
But if the barcode field does not exist in the database, I assumed it would be undefined
and it would still work. But it doesn't.. I tried to google this but couldn't find an explanation..
If the above question doesn't anger you (as my previous query did) and you can treat it as a genuine query, I would appreciate some guidance..
Here's the button
<button class="btn ticket__print-ticket" data-id=${user._id} data-barcode=${user.barcode}> Print Ticket </button>
</div>
Here's the argument being sent on event
if (modalContent) {
modalContent.addEventListener("click", function(e) {
e.preventDefault();
if (e.target.closest(".ticket__print-ticket")) {
const printTicketBtn = e.target;
const id = printTicketBtn.dataset.id;
const barcode = printTicketBtn.dataset.barcode;
console.log(id, barcode);
printTicket(barcode);
}
And here's the code that does not respond when the database field does not exist (even though it shows as undefined
when I console.log it
export const printTicket = async function(barcode) {
// console.log(barcode)
if (!barcode) {
console.log("in");
showMessage(
"Ticket Not Ready",
"The ticket process requires approval and should be completed soon. You will have a response soon. However you can show the QR Code at the Entrance and generate the ticket immediately"
);
return;
}