Compare the value in the databases and then do a specific action

I have two databases A + B and the function of the code is that it compares the values in database A and finds them, but the condition in the code is that it does not save in database B in case the number matches the numbers in database A, my problem is that the code finds the matching value And it saves and I want to save only in case the values do not match… Is there a solution?

Note: The code works correctly only with the first value in database A (number database)

this is My Code 👇🏻
$w.onReady(() => { ///--------------------------------------------------
    $w("#dataset1").onReady(async () => {
        let length = await $w('#dataset1').getTotalCount();
        console.log("Data-Length: ", length)

        $w('#button83').onClick(async () => {
            console.log("Click")
            let code = await ($w('#input24').value).toString();
            console.log("Input-Code: ", code)
            let data = await ($w("#dataset1").getItems(0, length));
            console.log("Data: ", data)
            for (var i = 0; i < length; i++) {
                let dataCode = String(data.items[i].Numbers);
                console.log("CODE: ", dataCode)
                if (dataCode === code) {
                    console.log("CODE found!")
                    $w('#button82').expand();
                    $w('#button83').collapse();
                    $w('#text168').text = "The membership number is correct, please press subscribe"
                    //$w('#text170').show();
                    $w('#box2').hide();
                    break;
                } else {
                    let toInsert = {
                        "name": $w('#input23').value,
                        "email": $w('#input21').value,
                    };
                    wixData.insert("WrongReg", toInsert)
                        .then(() => {
                            console.log("New User-Data saved.")
                            // here your CODE to disable BUTTON after the user was added to the DATABASE.
                            $w('#button82').disable();
                            $w('#button83').disable();
                            $w('#box2').show();
                            $w('#button82').collapse();
                            $w('#text168').show();
                            $w('#text168').text = "The membership number is incorrect"
                            $w('#button83').expand();
                        })
                        .catch((err) => { let errorMsg = err;
                            console.log(errorMsg); });
                }
            }
        });
    });
});
export function button84_click() {

    $w('#box2').collapse()

}

any help

Here is the solution of your problem.I have separated the insert section code into a function insertWrongReg. You can remove your code. First you are loading dataset which slow down the page speed… then using for loop which again need more time to execute the functionality.I am direct checking if the number exist in or not.
//insert wrongreg event
function insertWrongReg ( )
{
let toInsert = {
“name” : $w ( ‘#input23’ ). value ,
“email” : $w ( ‘#input21’ ). value ,
};
wixData . insert ( “WrongReg” , toInsert )
. then (() => {
console . log ( “New User-Data saved.” )
// here your CODE to disable BUTTON after the user was added to the DATABASE.
$w ( ‘#button82’ ). disable ();
$w ( ‘#button83’ ). disable ();
$w ( ‘#box2’ ). show ();
$w ( ‘#button82’ ). collapse ();
$w ( ‘#text168’ ). show ();
$w ( ‘#text168’ ). text = “رقم العضوية المدخل غير صحيح”
$w ( ‘#button83’ ). expand ();
console . log ( “wajid” );
})
. catch (( err ) => { let errorMsg = err ;
console . log ( errorMsg ); });
}

export function button83_click ( event ) {
console . log ( “Click” );
let code = Number ( $w ( “#input24” ). value );
console . log ( “Input-Code: " , code )
/* let data = await ($w(”#dataset1").getItems(0, length));
console.log("Data: ", data);*/
//for (var i = 0; i < length; i++) {
// let dataCode = String(data.items[i].Numbers);
// console.log("CODE: ", dataCode)
wixData . query ( “MembershipNumber” )
. eq ( “Numbers” , code )
. find ()
. then (( results )=>{
if ( results . items . length > 0 ){
let item = results . items [ 0 ];
console . log ( “found” , item );
if ( item . Numbers === code ) {
console . log ( “CODE found!” )
$w ( ‘#button82’ ). expand ();
$w ( ‘#button83’ ). collapse ();
$w ( ‘#text168’ ). text = “:point_down:t2: رقم العضوية المدخل صحيح الرجاء الضغط على اشتراك”
//$w(‘#text170’).show();
$w ( ‘#box2’ ). hide ();
//break;
} else {
insertWrongReg ();
}

            }  **else**  { 
                // not found 
                console . log ( "not found" ); 
                insertWrongReg (); 
            } 
           }); 
            
        //} 

}