Question: How do i set an “if else statement” for undefined values
I’m using wix editor for my site. I am trying to calculate the total amount paid by a customer. however, it will throw off an undefined value if this customer has never made any payment before. I have 2 database here.
database 1 - record customer details and how much he owe us and how much he should pay every month.
database 2 - this is a database for all payment received by all customers.
both database uses a case “ID NO” to identify whichever customer and whatever payment.
How can i create an if else statement that can check if this particular customer has no record of payment before, then we can set the amount paid to 0 instead of throwing off undefined value?
async function findtotalpaid(debtorID) {
const filter = wixData.filter().eq("idNo", debtorID);
let amountpaid = await wixData.aggregate("TigerCapitalPaymentHistory").filter(filter).sum("amount").count().run();
let noofpayment = amountpaid.items[0].count;
console.log("no of payment is " + String(noofpayment) + " for " + debtorID)
if (noofpayment > 0) {
var totalpaid = Number(amountpaid.items[0].amountSum);
} else if (!noofpayment || isNaN(noofpayment)) {
var totalpaid = 0;
}
return totalpaid
}
Hi Kevin! If I understand correctly, your code isn’t working because amountpaid returns undefined when the wixData query can’t find anything.
You can handle this in a couple of ways, one of which is using a try and catch block. Start with setting the amountpaid and totalpaid variables to 0 by default. So only if the aggregate function is able to run, the rest of the “try” block will run. If the await wixData.aggregate line fails, it will instead skip over the rest of the try block and log your error in the catch block. Something like this:
async function findtotalpaid(debtorID) {
const filter = wixData.filter().eq("idNo", debtorID);
let totalpaid = 0;
let amountpaid = 0;
try {
await wixData.aggregate("TigerCapitalPaymentHistory").filter(filter).sum("amount").count().run();
let noofpayment = amountpaid.items[0].count;
console.log("no of payment is " + String(noofpayment) + " for " + debtorID)
if (noofpayment > 0) {
totalpaid = Number(amountpaid.items[0].amountSum);
} else if (!noofpayment || isNaN(noofpayment)) {
totalpaid = 0;
}
} catch (error) {
console.log("no entry found: ", error);
}
return totalpaid;
}
Hi emmy i tried what you said. but it is still coming out as ok. i think the value is being returned as undefined. And this causes the value when i show it to the front page being ‘NaN’. or is it caused by something else in the code?
Here i attached the code for the page. I uses “populatedetails()” inside “onready()”. the totalpaid value is used inside the “populatedetails()” function. Can you help to have a look? thank a lot once again.
function populatedetails() {
$w(“#dataset1”).onReady(() => {
$w(“#repeater1”).onItemReady(($item, debtor, ) => {
let loanamount = debtor.loanAmount;
let installment = debtor.loanInstallment;
let tenure = debtor.loanTenure;
let debtorID = debtor.idNo;
let monthly = debtor.loanInstallment;
let paymentstartmonth = debtor.paymentCommencementDate;
function formatNumber(num) {
return num
.toFixed(2) // always two decimal digits
.toLocaleString()
.replace(/\B(?=(\d{3})+(?!\d))/g, “,”);
}
async function findtotalpaid(debtorID) {
const filter = wixData.filter().eq(“idNo”, debtorID);
var totalpaid = 0;
try {
let amountpaid = await wixData.aggregate(“TigerCapitalPaymentHistory”).filter(filter).sum(“amount”).count().run();
let noofpayment = amountpaid.items[0].count;
console.log("no of payment is " + String(noofpayment) + " for " + debtorID)
if (noofpayment > 0) {
var totalpaid = Number(amountpaid.items[0].amountSum);
} else {
var totalpaid = 0;
}
} catch (error) {
console.log("no entry found for " + debtorID + ": ", error);
var totalpaid = 0;
}
return totalpaid;
}
function findNoOfMonthTillNow(olddate) {
let year1 = new Date(olddate).getFullYear();
let month1 = new Date(olddate).getMonth();
let year2 = new Date().getFullYear();
let month2 = new Date().getMonth();
Hey Kevin, you can also try this simple trick of adding || 0 to the end of the line which should automatically set the value to 0 if the condition before returns null or undefined. This was the workaround I used on one of my sites where I was facing a similar issue, and this might work for you as well.