How to force a clause to be performed only after returning from a backend function?
I call a backend function but I see that the following clause on the main code is performed before the function returned. How can I force the execution clause order. Please see me below code
In the below code, the Following clauses are performed before returning from the “getusers” function.
"rtusers = tusers
console . log ( “results after return from getusers==>” , rtusers)
Code calling the function
import { getuser } from ‘backend/getuser’ ;
export function button1_click ( event ) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
// Add your code for this event here:
var rtusers =
getuser ( $w ( “#input1” ). value )
. then ( tusers => {
rtusers = tusers
console . log ( “results after return from getusers==>” , rtusers )
})
}
Backend function
import wixData from ‘wix-data’ ;
//import { sql } from ‘@velo/wix-data-sql-backend’;
export function getuser ( parshem ) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
// Add your code for this event here:
var results =
try {
//const results = sql('SELECT _id, email FROM user WHERE shem = " + parshem" ');
wixData . query ( “users” )
. eq ( “shem” , parshem )
. find ()
. then (( results ) => {
if ( results . items . length > 0 ) {
console . log ( “inside getuser backend” , parshem )
console . log ( “results==>” , results )
return results
}
})
} catch ( error ) {
console . error ( error );
}
}