Data Query retrieve /check/insert/remove

hello,

My question:
I am probably overcomplicating this… I am trying to create a UI that allows my customers to check a value against my dataset (to see if the any of the fields contain it).

  1. uid = user “password”: i test against all entries in the dataset to see if any contain this “password”. if they do, i proceed to next logic; if not, change text to show none found

  2. certID = stored as a string, not an int : if logic above clears, i then want to search that specific id, grab the array where all strings should be stored & test against that string to see if it contains the value they entered.

  3. if all above clears, i then want to remove the reference from the array mentioned in #2 and add the value to a different array field in the dataset.

hopefully this was explained ok. all code entered is just for testing and contains no personal information*

the page contains two user inputs and a button… as well as a few groups and labels but ignore those.

import wixData from 'wix-data';

$w.onReady(function () {

});

export function button1_click(event) {

 let certID = $w('#input1').value;
 let userPass = $w('#input2').value;
 let found = false;
 let counter = 0, id =0;
 
    wixData.query("qrdotd") 
        .find()
        .then((results) => {
 while (!found)
            {
                console.log("running loop for the " + counter + " time")
 if (results.items[counter].businessUniqueID === userPass)
                {
                    found = true;
                    console.log("result has successfully recognized UID in dataset. Proceeding.")
                    id = results.items[counter]._id;
                    console.log("the entry id is " + id)
                }
 else if (results.items[counter].businessUniqueID === null)
                {
                    console.log("result has returned null")
                    $w('#group83'.hide()) //main
                    $w('#button2'.show()) //submit again btn
                    $w('#text20'.text = "We could not match your business ID with any in our system.")  //text error
                    $w('#text20'.show())  //text show
                }
 else 
                    counter++
                    console.log("Counter is now at: " + counter)
            }
        })
 
 if (found)
    {
        wixData.get("qrdotd", id)
            .then( (res) => {
 let items = res.activeDealCertificateNumbers;
 for (let x = 0; x < items.count; x++)
                {
                    console.log(items[x] + " is a valid cert #")
                }
 if (items.contains("activeDealCertificateNumbers", certID))
                {
                    $w('#group83'.hide()) //main
                    $w('#button2'.show()) //submit again btn
                    $w('#text21'.text = "Certificate# is valid and should be accepted.")  //text error
                    $w('#text21'.show())  //text show
                    wixData.removeReference("qrdotd", "activeDealCertificateNumbers", id, certID)
                    wixData.insertReference("qrdotd", "usedDealCertficateNumbers", id, certID)
                    console.log("please check references in dataset. should be inserted & removed ")
                }
 
            })
            .catch( (err) => {
 let errorMsg = err;
                console.log (errorMsg)
            } );
    }
 else {
        console.log("found is = " + found)
    } 
}

my test dataset:

console when debugging:

the loop runs twice and I am assuming it breaks itself, after “running loop for the 1 time” it stops permanently unless I click the button again.

Any ideas?