Help with IF statement

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

anyone ?

Hi Shan,

What exactly does not work? Please provide more details about what happens.

As for the code:

  1. You need to handle the case when item is not found - so the variable Item will not be undefined

  2. 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 !

Hi @shan I’m trying to implement something similar but have an error for the

getDis(); 

Can you show me where this is defined? Many thanks!

@stephenmccall

This is my function to calculate Discount. I have not shown it in the above code as it was irrelevant to my problem.

@shantanukumar847 Thanks, I appreciate this is the calculation for the actual price discount. I now have everything working apart form this calculation, would you be able to help with the code? I appreciate if it is your own and if you don’t want to disclose. I have been getting along well with the coding but arithmetic and application are a new venture for me! I have the following code so far but is obviously not working…

function getDis (results) {
let Item = results.items[0];
if (Item && Item.discountCode === $w("#coupon").value){
    ($w("#amount").value = $w("#amountText").text * String(Item.value) / 100);
    }    
}

@stephenmccall Hi

Please see what I did below:

function decideDiscount(results) {
 let Item = results.items[0];
 if (Item && Item.discountCode === $w("#coupon").value) {
        $w("#invalidCode").hide(); //hide invalid code alert
        $w("#discount").text = String(Item.value); //get the value ready to calculate it
        $w("#discountbox").text = String(Item.value) + "% DISCOUNT APPLIED"; ////put the discount value in a text to show it to the user
        getDis(); //calculate and update total
    } else {
        $w("#invalidCode").show();
        $w("#discountLoader").hide();
    }
}

async function getDis() {
 let valueAfterDiscount = (Number($w("#finalAmount").text)) - ((Number($w("#finalAmount").text/100)) * (Number($w('#discount').text)));
 await $w("#discountbox").show();
    $w("#discountLoader").hide();
    $w("#couponGo").disable();
    $w('#finalAmount').text = parseFloat(String(valueAfterDiscount)).toFixed(2);
}

Good Luck !

@shantanukumar847 Thank you so much for this! It works like a charm :slight_smile: