IF ELSE IF statement repeating twice

I am trying to make a billing solution. Here is a sample barcode you can use:
9789353163150.
I have 30 rows (not a table,manually built using input elements) and the barcode retrieves data from a dataset and the values get allocated to the empty rows.
So, I am using a if else if loop to check if field is used or not but on input1.onchange, it seems like the loop runs twice and same data appears in 2 consecutive rows. Please help. Where am I wrong?

The below code is only for 2 rows. If problem is solved will add all 30.


Code:

import wixData from ‘wix-data’;
$w.onReady( function () {
$w(‘#input342’).focus();
});
export function input342_change(event) {
var invser
var newinvser
invser = parseInt($w(‘#invseries’).value);
$w(‘#dataset14’).setFilter(wixData.filter().eq(‘phone’, $w(‘#input342’).value));
newinvser = invser + 1;
$w(‘#input340’).value = ‘AAW’ + newinvser;
$w(‘#input1’).focus();
}
export function cancel1_click(event) {
$w(‘#id1’).value = “”;
$w(‘#an1’).value = “”;
$w(‘#hsn1’).value = “”;
$w(‘#qty1’).value = “”;
$w(‘#p1’).value = “”;
$w(‘#dp1’).value = “”;
$w(‘#da1’).value = “”;
$w(‘#na1’).value = “”;
$w(‘#tp1’).value = “”;
$w(‘#ta1’).value = “”;
$w(‘#netamt1’).value = “”;
($w(‘#dataset12’).setFilter(wixData.filter().eq(‘productBcid’, $w(‘#input1’).value)))
let itemObj = $w(“#dataset12”).getCurrentItem();
$w(“#dataset12”).setFieldValue(“qtyS”, itemObj.qtyS - 1);
$w(‘#dataset12’).save().then(() => { $w(‘#dataset12’).refresh(); });
$w(‘#input1’).focus()
}

export function input1_change(event) {
($w(‘#dataset12’).setFilter(wixData.filter().eq(‘productBcid’, $w(‘#input1’).value))).then(()=>{
let itemObj1 = $w(“#dataset12”).getCurrentItem();
$w(“#dataset12”).setFieldValue(“qtyS”, itemObj1.qtyS + 1);

if ($w(‘#an1’).value === “”) {
$w(‘#id1’).value = itemObj1.productBcid;
$w(‘#an1’).value = itemObj1.name;
$w(‘#hsn1’).value = itemObj1.hsn;
$w(‘#p1’).value = itemObj1.price;
$w(‘#qty1’).value = $w(‘#mqty’).value;
$w(‘#dp1’).value = itemObj1.discount;
$w(‘#da1’).value = itemObj1.discnetamt;
$w(‘#na1’).value = itemObj1.netamt;
$w(“#tp1”).value = itemObj1.tax;
$w(‘#ta1’).value = itemObj1.taxamt;
$w(“#netamt1”).value = itemObj1.finalamt;
$w(‘#dataset12’).save();
newfield();

    } 

else if ($w(‘#an2’).value === “”) {
$w(‘#id2’).value = itemObj1.productBcid;
$w(‘#an2’).value = itemObj1.name;
$w(‘#hsn2’).value = itemObj1.hsn;
$w(‘#p2’).value = itemObj1.price;
$w(‘#qty2’).value = $w(‘#mqty’).value;
$w(‘#dp2’).value = itemObj1.discount;
$w(‘#da2’).value = itemObj1.discnetamt;
$w(‘#na2’).value = itemObj1.netamt;
$w(“#tp2”).value = itemObj1.tax;
$w(‘#ta2’).value = itemObj1.taxamt;
$w(“#netamt2”).value = itemObj1.finalamt;
$w(‘#dataset12’).save();
newfield();
}
else { }

   }) 



} 

function newfield () {
$w(‘#input1’).focus();
$w(‘#input1’).value = null ;

}

The link to the page is here :

https://websiteforatom.wixsite.com/aawrun/copy-of-pos-test

Hi, Sagar.

I recommend that you first confirm that the 2nd bar code has a different value than the 1st bar code. To check the two values, you could insert:

console.log("itemObj1.productBcid = " + itemObj1.productBcid);

immediately before the following line:

  if ($w('#an1').value === "") {

and then check the (Developer Tools) console after running your code.

HTH,

Art Bergquist

Thank you for the suggestion. Tried it. The console gets logged two times. Here is the output:


itemObj1.productBcid = 9789353163150

Hook afterQuery for collection products result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined]

itemObj1.productBcid = 9789353163150

What I am trying to check is whether fields are empty or not from an1 to an30. The values retrieved from database are then put in the row which is blank. That is why I am using IF ELSE IF statement. I am not understanding why this is happening. My code should stop after satisfying one if condition. It just also moves on to the next one.

Just for conformation that the loop is running twice, I added some console logs. It gave me the folllowing result:

itemObj1.productBcid = 9789353163150

first if ran successfully

Hook afterQuery for collection products result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined]

Hook afterQuery for collection products result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined]

itemObj1.productBcid = 9789353163150

second if ran successfully

Revised code:

import wixData from ‘wix-data’;
$w.onReady( function () {
$w(‘#input342’).focus();
});
export function input342_change(event) {
var invser
var newinvser
invser = parseInt($w(‘#invseries’).value);
$w(‘#dataset14’).setFilter(wixData.filter().eq(‘phone’, $w(‘#input342’).value));
newinvser = invser + 1;
$w(‘#input340’).value = ‘AAW’ + newinvser;
$w(‘#input1’).focus();
}
export function cancel1_click(event) {
$w(‘#id1’).value = “”;
$w(‘#an1’).value = “”;
$w(‘#hsn1’).value = “”;
$w(‘#qty1’).value = “”;
$w(‘#p1’).value = “”;
$w(‘#dp1’).value = “”;
$w(‘#da1’).value = “”;
$w(‘#na1’).value = “”;
$w(‘#tp1’).value = “”;
$w(‘#ta1’).value = “”;
$w(‘#netamt1’).value = “”;
($w(‘#dataset12’).setFilter(wixData.filter().eq(‘productBcid’, $w(‘#input1’).value)))
let itemObj = $w(“#dataset12”).getCurrentItem();
$w(“#dataset12”).setFieldValue(“qtyS”, itemObj.qtyS - 1);
$w(‘#dataset12’).save().then(() => { $w(‘#dataset12’).refresh(); });
$w(‘#input1’).focus()
}

export function input1_change(event) {
($w(‘#dataset12’).setFilter(wixData.filter().eq(‘productBcid’, $w(‘#input1’).value))).then(()=>{
let itemObj1 = $w(“#dataset12”).getCurrentItem();
$w(“#dataset12”).setFieldValue(“qtyS”, itemObj1.qtyS + 1);

    console.log("itemObj1.productBcid = " + itemObj1.productBcid); 

if ($w(‘#an1’).value === “”) {
$w(‘#id1’).value = itemObj1.productBcid;
$w(‘#an1’).value = itemObj1.name;
$w(‘#hsn1’).value = itemObj1.hsn;
$w(‘#p1’).value = itemObj1.price;
$w(‘#qty1’).value = $w(‘#mqty’).value;
$w(‘#dp1’).value = itemObj1.discount;
$w(‘#da1’).value = itemObj1.discnetamt;
$w(‘#na1’).value = itemObj1.netamt;
$w(“#tp1”).value = itemObj1.tax;
$w(‘#ta1’).value = itemObj1.taxamt;
$w(“#netamt1”).value = itemObj1.finalamt;
$w(‘#dataset12’).save();
newfield();
console.log(“first if ran successfully”);

    } 

else if ($w(‘#an2’).value === “”) {
$w(‘#id2’).value = itemObj1.productBcid;
$w(‘#an2’).value = itemObj1.name;
$w(‘#hsn2’).value = itemObj1.hsn;
$w(‘#p2’).value = itemObj1.price;
$w(‘#qty2’).value = $w(‘#mqty’).value;
$w(‘#dp2’).value = itemObj1.discount;
$w(‘#da2’).value = itemObj1.discnetamt;
$w(‘#na2’).value = itemObj1.netamt;
$w(“#tp2”).value = itemObj1.tax;
$w(‘#ta2’).value = itemObj1.taxamt;
$w(“#netamt2”).value = itemObj1.finalamt;
$w(‘#dataset12’).save();
newfield();
console.log(“second if ran successfully”)
}
else { }

   }) 



} 

function newfield () {
$w(‘#input1’).focus();
$w(‘#input1’).value = null ;

}