Query not running at all

Hi everyone, I got a strange problem, tried to make an additional database with user emails and some info about them, so 1st step was to check if it has already email in it. Used query, .eq and .find to search for it and it not working at all, I put toInsert.whois = something(found, not found, err) in all possible places which could query run and none of them run, it’s always write in database standard “b4 run” which is set before query should works. The second part with .insert is working nice.

import wixData from ‘wix-data’;
$w.onReady( function () {
});
export function button1_click(event) {
const mail = $w(‘#text1’).text;
let toInsert = {
“title”: mail,
};
toInsert.whois = “b4 run”;
wixData.query(“AdditionalMembersInfo”)
.eq(“title”, mail)
.find()
.then( (results) => {
if (results.items.length > 0) {
toInsert.whois = “found”;
let firstItem = results.items[0]; //see item below
} else {
toInsert.whois = “not found”;
}
} )
. catch ( (err) => {
toInsert.whois = “err”;
let errorMsg = err;
}
);
wixData.insert(“AdditionalMembersInfo”, toInsert)
.then( (results) => {
let item = results; //see item below
}
)
. catch ( (err) => {
let errorMsg = err;
}
);
//Add your code for this event here:
}

Better formatted screenshot. So button click always write only 1st option and not 2-4 while it should rewrite field in any possible scenario if that query code was run.

It’s because you send the insert before the results of the query come back.

https://support.wix.com/en/article/corvid-working-with-promises

Either label the click handler async and await the query result or move the insert into the promise chain.