Showing Buttons based on Member Roles

Made this code to hide a button if a person does not have a specific member role. Works and thought someone might find it useful.

any tips to improve also welcomed.


import { currentMember } from 'wix-members';

$w.onReady(function(){
    currentMember.getRoles()
        .then( (roles) => {
                let isVolunteer = false;
      
                for (var i = 0; i < roles.length; i++) {
                    var firstRole = roles[i];
                    var roleID = firstRole._id;
                if(roleID === "xxxx"){
                isVolunteer = true;
                }

                if(isVolunteer) {
                    $w('#button29').expand();
                } else {
                    $w('#button29').collapse();
                }
           }});});

3 Likes

If you’re looking for a specific role, you can do it with shorter code.
Instead of this for loop, you can do:

isVolunteer  = !!roles.filter(e => e._id === 'xxxx').length;

It filters the roles and only leaves the matching value, then it checks if the filtered array has length or doesn’t have length.

1 Like

Nice job!
Where do you go to get the e . _id === ‘xxxx’?

I found it.
It’s under “Site Members” → Member Permissions