ELSE IF STATEMENTS FIRST 2 WORK 3rd DOESN'T?

Hello team wix, I’ve got my else if statements working for the first 2 but the third else if statement (totalCount === 0) does nothing… There is no dataset being used and the code is on a lightbox. I must be messing up somewhere any suggestions? Thanks in advance!

export function customerlogin_click ( event ) {
let email = $w ( “#email” ). value ;
let userEmail = $w ( “#email” ). value ;
let password = $w ( “#password” ). value ;

wixData . query ( ‘VehicleDiagnose’ )
. eq ( ‘email’ , $w ( ‘#email’ ). value )
. or ( wixData . query ( ‘VehicleDiagnose’ )
. eq ( ‘userEmail’ , $w ( ‘#email’ ). value ))
. find ()
. then (( results ) => {
let resultCount = results . totalCount ;
if ( results . items [ 0 ]. email === $w ( ‘#email’ ). value ) {
wixUsers . login ( email , password )
. then ( () => {
console . log ( “User is logged in” );
wixLocation . to ( /drivers-dashboard );
})
} else if ( results . items [ 0 ]. userEmail === $w ( ‘#email’ ). value ) {
wixUsers . login ( email , password )
. then ( () => {
console . log ( “User is logged in” );
wixLocation . to ( /vehicle-diagnose/ ${ userEmail } /home );
})
} else if (totalCount === 0){
$w ( ‘#errortext’ ). show ();
}
})

. catch ( ( err ) => {
console . log ( err );
});
}

I’ve also tried without count and just the else statement at the end

} else {
$w ( ‘#errortext’ ). show ();
}
})

. catch ( ( err ) => {
console . log ( err );
});
}

And still doesn’t work (‘#errortext’) won’t show. But the first two wixLocation.to work fine.

Can you put in a code block? (hard to read it now)

This is a code block

You have to change totalCount to resultCount OR results.totalCount

the totalCount hasn’t been defined in your code, so you have to change it or define it.

so in your case, totalCount will go to undefined if you dont change your code. Hence, it wont be a number or string (undefined)

BIG THANKS for your reply! Let me make the change and try now

I tried both resultCount but still nothing also tried resuts.totalCount and no change… also this is how the code formats when I paste in a code block

export function customerlogin_click ( event ) {
let email = $w ( “#email” ). value ;
let userEmail = $w ( “#email” ). value ;
let password = $w ( “#password” ). value ;

wixData . query ( ‘VehicleDiagnose’ )
. eq ( ‘email’ , $w ( ‘#email’ ). value )
. or ( wixData . query ( ‘VehicleDiagnose’ )
. eq ( ‘userEmail’ , $w ( ‘#email’ ). value ))
. find ()
. then (( results ) => {
let resultCount = results . totalCount ;
if ( results . items [ 0 ]. email === $w ( ‘#email’ ). value ) {
wixUsers . login ( email , password )
. then ( () => {
console . log ( “User is logged in” );
wixLocation . to ( /drivers-dashboard );
})
} else if ( results . items [ 0 ]. userEmail === $w ( ‘#email’ ). value ) {
wixUsers . login ( email , password )
. then ( () => {
console . log ( “User is logged in” );
wixLocation . to ( /vehicle-diagnose/ ${ userEmail } /home );
})
} else if ( results . totalCount === 0 ) { //also tried (results.totalCount === 0)
$w ( ‘#signup’ ). label = ‘No record found! Signup here >’ ;
}
})
. catch ( ( err ) => {
console . log ( err );
});
}

Stumped with this one… does the code above look accurate or am I missing something?

Ok so, I changed the code and now it works… in case anyone has a similar issue here’s the working code:

export function customerlogin_click ( event ) {
let email = $w ( “#email” ). value ;
let userEmail = $w ( “#email” ). value ;
let password = $w ( “#password” ). value ;

wixData . query ( ‘VehicleDiagnose’ )
. eq ( ‘email’ , $w ( ‘#email’ ). value )
. or ( wixData . query ( ‘VehicleDiagnose’ )
. eq ( ‘userEmail’ , $w ( ‘#email’ ). value ))
. find ()
. then (( results ) => {

let resultCount = results . totalCount ;

if ( resultCount === 0 ) {
$w ( ‘#signup’ ). label = ‘No record found! Signup here >’ ;

} else if ( results . items [ 0 ]. email === $w ( ‘#email’ ). value ) {
wixUsers . login ( email , password )
. then ( () => {
console . log ( “User is logged in” );
wixLocation . to ( /drivers-dashboard );
})
} else if ( results . items [ 0 ]. userEmail === $w ( ‘#email’ ). value ) {
wixUsers . login ( email , password )
. then ( () => {
console . log ( “User is logged in” );
wixLocation . to ( /vehicle-diagnose/ ${ userEmail } /home );
})
}
})
. catch ( ( err ) => {
console . log ( err );
});
}

By changing the first if statement to check resultCount it now works as desired. Thanks again for your time and suggestion!!

1 Like

My pleasure :slight_smile: