I have multiple roles defined and want to retrieve the roles for a given user. The particular user I am testing has 2 roles. This works fine OUTSIDE of a for loop which is the commented out section of the below code.
However it DOES NOT work when the exact same code is placed inside of the below for loop. Why??
Here is the code:
//####### getRoles() ONLY WORKs ON LIVE Site (NOT Preview)
//get user Roles
let userroles = user.getRoles()
.then( (roles) => {
let numRoles = roles.length;
$w( “#numRoles” ).value = numRoles;
if (numRoles > 0 ) {
/*
// ######### WORKS #########
let firstRole = roles[0];
$w("#userrole1").text = firstRole.name;
let secondRole = roles[1];
$w("#userrole2").text = secondRole.name;
*/
// ######### forLoop DOES NOT WORK #########
**for** ( var i = 0 ; i < numRoles.length; i++) {
if (i === 0 ) {
let firstRole = roles[ 0 ];
$w( "#userrole1" ).text = firstRole.name;
}
if (i === 1 ) {
let secondRole = roles[ 1 ];
$w( "#userrole2" ).text = secondRole.name;
}
}
}
})
. catch ( (error) => {
console.log(error);
} );