Unable to get button to conditionally hide using .getRoles() and arrays

I’ve spent more than a week trying to figure out how to show and hide buttons on dynamic pages based on these criteria:

  1. the visitor must be logged in

  2. the visitor’s role (i.e “Gold”, “Silver”, “Bronze”) must match the ‘Access Conditions’ I set.

Each dynamic has year information (the ‘pageYear’). I want to restrict access on certain dynamic pages based on this year information and the user role (by Wix). Here are the conditions:

Role access Conditions
Gold - access to dynamic pages that contain the years: 1970 to the current year
Silver - access to dynamic pages that contain the years: 2011 to current year
Bronze - access to dynamic pages that contain the years: 2011 to last year

I tried writing code (see below) but am unable to make the button show or hide properly. For some reason, the button is always hidden, leading me to think that maybe I’m not calling the user role (e.g. Bronze, Silver, Gold) correctly. I’ve followed a lot of Wix forum posts and I’ve tried them all – nothing seems to be working. I need help!

I have already:

  1. Checked it using the live site. ( I always do)

  2. Looked at the Wix API references

  3. Set up the member roles such that it appears on the member list:

Here is my code:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
let source;
let user = wixUsers.currentUser;

$w.onReady( function () {
$w(“#dynamicDataset”).onReady( () => {
let itemObj = $w(“#dynamicDataset”).getCurrentItem();
let pageYear = itemObj.year
console.log(pageYear)
var today = new Date();
var currentYear = today.getFullYear();
var lastYear = today.getFullYear() - 1;
if (user.loggedIn) {
user.getRoles()
.then((roles) => {
let firstRole = roles[0];
let roleName = firstRole.name;

                          //Checks under which tier/roles the user falls and then forms the array 
                          **var**  tier = roleName; 
                          **var**  arr; 
                          **if**  (tier === "Bronze") { 
                                  **var**  t1 = []; 
                                  **var**  t1_start; 
                                  **for**  (t1_start = 2011; t1_start <= lastYear; t1_start++) { 
                                          t1.push(t1_start); 
                                  } 
                                  arr = t1; 

                           }  **else if**  (tier === "Silver") { 
                                  **var**  t2 = []; 
                                  **var**  t2_start; 
                                  **for**  (t2_start = 2011; t2_start <= currentYear; t2_start++) { 
                                           t2.push(t2_start); 
                                  } 
                                  arr = t2; 

                            }  **else if**  (tier === "Gold") { 
                                  **var**  t3 = []; 
                                  **var**  t3_start; 
                                  **for**  (t3_start = 1970; t3_start <= currentYear; t3_start++) { 
                                          t3.push(t3_start); 
                                  } 
                                  arr = t3; 
                          }  **else**  { 
                              } 

//Checks if the pageYear is part of the array. If yes, show; otherwise, hide.
function tier_1() {
if (arr.includes(pageYear)) {
$w(“#button9”).show();
} else {
$w(“#button9”).hide();
}
}
//Passes the value of array arr and runs the function
tier_1(arr);
});
} else {
$w(“#button9”).hide();
}
} );
});

What am I doing wrong? I would really appreciate any help!

1 Like

I’m so confused. Even a simple conditional is not working. This really confirms my doubts that the problem lies in the acquisition of user roles.

Push… Really need help, guys! :frowning:

Were you able to get any help with this? I am having a similar issue whereby I am trying to display buttons based on three conditions - logged in, assigned a specific role; logged in; no role assigned; not logged in. I have the logged in/not logged in working perfectly but the buttons display as though no role was assigned in all logged in cases.