Hey guys,
I am running a query to see if a value entered by the user exists in the database column or not. If it does not exist then I would like to display a hidden text and hide an image. I tried the below 2 codes but it does not work.
$w("#discountLoader").show();
wixData.query("DiscountCoupons")
.eq('discountCode', $w("#coupon").value)
.find()
.then( (results) => {
decideDiscount(results);
});
}
function decideDiscount(results) {
let Item = results.items[0];
if (Item.discountCode === $w("#coupon").value) {
$w("#discount").text = String(Item.value);
$w("#discountbox").text = String(Item.value) + "% DISCOUNT APPLIED";
getDis();
} else if (Item.discountCode.length === 0) {
$w("#invalidCode").show();
$w("#discountLoader").hide();
}
}
And this one neither
$w("#discountLoader").show();
wixData.query("DiscountCoupons")
.eq('discountCode', $w("#coupon").value)
.find()
.then( (results) => {
decideDiscount(results);
});
}
function decideDiscount(results) {
let Item = results.items[0];
if (Item.discountCode === $w("#coupon").value) {
$w("#discount").text = String(Item.value);
$w("#discountbox").text = String(Item.value) + "% DISCOUNT APPLIED";
getDis();
} else if (Item.discountCode !== $w("#coupon").value) {
$w("#invalidCode").show();
$w("#discountLoader").hide();
}
}
Can anyone help please
Hi Shan,
What exactly does not work? Please provide more details about what happens.
As for the code:
You need to handle the case when item is not found - so the variable Item will not be undefined
In general it is possible that an error occurs when you code is executed - you can try to debug it by wrapping the code within try-catch block and printing the error. Then you can test it in the Preview mode and check the output in the console.
So your second code example would look this way:
function decideDiscount(results) { try { let Item = results.items[0]; if (Item && Item.discountCode === $w("#coupon").value) { $w("#discount").text = String(Item.value); $w("#discountbox").text = String(Item.value) + "% DISCOUNT APPLIED"; getDis(); } else { $w("#invalidCode").show(); $w("#discountLoader").hide(); } } catch (error) { console.log(`Error occurred: ${error}`) } }
This is perfect Alex !
Thank you so much for your help !
anyone ?