Using multiple if statements to decide what button is displayed.

I’m trying to specify what buttons appear on a page based on two factors. The first is whether the user is logged in and the second is whether, in the user database, the field ‘type’ matches either of the words ‘temp’ or ‘client’ or is empty. The code I have at the moment to differentiate between logged in or not logged in is:

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#login1”).label = “Logout”;
$w(“#account1”).show();
$w(“#account2”).show();
}
else {
$w(“#login1”).label = “Login”;
$w(“#account1”).hide();
$w(“#account2”).hide();
}
} );

This works.

However, the variations I would like are as follows:

User:logged in && Field ‘type’ is empty
$w(“#login1”).label = “Logout”;
$w(“#account1”).hide();
$w(“#account2”).hide();

User:logged in && Field ‘type’ = ‘Client’
$w(“#login1”).label = “Logout”;
$w(“#account1”).show();
$w(“#account2”).hide();

User:logged in && Field ‘type’ = ‘Temp’
$w(“#login1”).label = “Logout”;
$w(“#account1”).hide();
$w(“#account2”).show();

User:logged out regardless of ‘type’
$w(“#login1”).label = “Login”;
$w(“#account1”).hide();
$w(“#account2”).hide();

I am unsure of:
a. how to code the if statement to reference a field in the database (currently called Users)
b. how to combine two if statements

Any help would be appreciated!

Hello

a → you can query the collection and see the results where the field equals the value.
b → you can use ‘&&’ expression of nested if statement or any other way u find suitable for the case.

here’s how i would suggest doing it if you have the user’s id in the database(if not you can use the field and value u want to search based on):

//you can call this function in $w.onReady()
async function show_buttons() {
 
 let logged_in = wixUsers.currentUser.loggedIn;

 let tybe_empty = await wixData.query("myCollection").eq('_id', wixUsers.currentUser.id).eq('users', 'empty').find();
 let tybe_clien = await wixData.query("myCollection").eq('_id', wixUsers.currentUser.id).eq('users', 'client').find();
 let tybe_temp = await wixData.query("myCollection").eq('_id', wixUsers.currentUser.id).eq('users', 'temp').find();

 if (logged_in) {
        $w("#login1").label = "Logout";

        if (tybe_empty) {
            $w("#account1").hide();
            $w("#account2").hide();
        } else if (tybe_clien) {
            $w("#account1").show();
            $w("#account2").hide();
        } else if (tybe_temp) {
            $w("#account1").hide();
            $w("#account2").show();
        }

    } else {
        $w("#login1").label = "Login";
        $w("#account1").hide();
        $w("#account2").hide();
    }

}

you can see here to know more about querying a collection.

best!
Massa