getRoles() Function is not working

This code is not working in my website at all:
import wixUsers from ‘wix-users’;

$w.onReady( function () {
wixUsers.currentUser.getRoles()
.then((roles) => {
console.log(roles[0])
})
});

I have three roles and i can’t get any

First off, note that Wix Users will only work properly in a live and published site, do not test it just through the preview.

The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.
The APIs in wix-users can only be used once the page has loaded. Therefore, you must use them in code that is contained in or is called from the onReady() event handler or any element event handler.

It will only work if the current user is logged in site member and only get one role, which if you have site members with multiple roles, it can’t guarantee which role is first and get that specific role.

As mentioned in this previous post here.
https://www.wix.com/corvid/forum/community-discussion/get-the-information-of-user-roles

You can read more about that in a previous post linked below, where they talk about getting members paid plans and if they have more than the one, which like getRoles is asynchronous code.
https://www.wix.com/corvid/forum/community-discussion/checking-the-member-plan-status-then-show-or-hide-button

You would need something like this…

import wixUsers from 'wix-users';

$w.onReady(function () {
var roleName;
let currentUser = wixUsers.currentUser;

currentUser.getRoles()
.then( (roles) => {
var firstRole = roles[0];
roleName = firstRole.name;
if(roleName === "Teachers"){
$w('#teachersText').show();
}
})
});

Have a look at the Wix Users getRoles function itself too.
https://www.wix.com/corvid/reference/wix-users.User.html#getRoles
Examples
Get a member’s roles

user.getRoles()
  .then( (roles) => {
    if(roles.length > 0) {
      let firstRole = roles[0];
      let roleName = firstRole.name;                // "Role Name"
      let roleDescription = firstRole.description;  // "Role Description"
    }
    else {
      console.log("Member has no roles");
    }
  } )
  .catch( (error) => {
    console.log(error);
  } );

/*  
 * Member with roles:
 *  Roles array is:
 *    [
 *      { 
 *        "name": "Role Name",
 *        "description": "Role Description."
 *      }, {
 *        "name": "Another Role",
 *        "description": "Another Description"
 *      }
 *    ]
 * 
 * Member with no roles:
 *   Logs: "Member has no roles"
 *   Roles array is: []
 * 
 * Admin:
 *   Roles array is:
 *     [
 *       { 
 *         "name": "Role Name",
 *         "description": "Role Description."
 *       }, {
 *         "name": "Admin"
 *       }
 *     ]
 * 
 * Non logged-in visitor: 
 *   Logs: "No user is currently logged in"
 */

See previous forum posts for more info.

So, GOS like to post links to answers and make you figure it out for yourself. which doesn’t always help answer the question
so here’s an actual working code that more directly solves the riddle

import wixUsers from 'wix-users';
$w.onReady(function () {
var roleName;
let currentUser = wixUsers.currentUser;
currentUser.getRoles()
.then((roles) => {
console.log('Received '+roles.length+' roles');
for (let i = 0 ; i < roles.length ; i++) {
console.log(roles[i]);
var roleType = roles[i];
roleName = roleType.name;
if(roleName === "Steward"){$w("#steward").expand(); $w('#steward').show(); }
if(roleName === "Official Judge"){$w("#judges").expand(); $w('#judges').show(); }
}
})
});

While your contribution is most welcome, your tone is quite uncalled for. There is no reason to go trashing others’ contributions.

This forum has zero tolerance for this sort of behavior. Please modify your post so that it conforms to the Community Guidelines . Continued behavior such as this can result in a user being banned. Thank you

Okay, so…
https://www.wix.com/corvid/forum/community-discussion/save-a-number-to-a-database-depending-on-which-tag-or-checkbox-group-is-selected-when-you-click-a-button

Plus, as Yisrael has already mentioned the Forum Guidelines, then please take note of this one.

No Freebies.
Don’t expect anyone to provide code snippets or full solutions; the community is here to help you learn how to solve problems.

Plus, on the given link example, there was already a previous forum user who had posted up a working sample that they had used, so this forum user could have used that to begin with.
https://www.wix.com/corvid/forum/community-discussion/use-role-not-accessible-on-code

Yes, it could easily be improved like as the example that you have provided above, however it was a starting point for the user to get on with.

Which again is in the Forum Guidelines too.

Posting guidelines:
Search Before Posting.
Use the Forum search. There’s a great chance that someone has already solved the problem that you are facing. If you post a question that has already been asked and answered, your post may be locked. You might find the answer to your question by searching the Corvid documentation.

================================================================

Hi below is my code I use when someone log and I want to show different menu for different group so user.getRoles() gives me roles

$w.onReady(() => {
let ktos = wixUsers.currentUser;
if (ktos.loggedIn) setupPage();
wixUsers.onLogin((user) => {
setupPage();
});
});

export function setupPage() {
let user = wixUsers.currentUser;
user.getRoles()
.then((roles) => {

if (roles.length === 0){
$w("#menu").show();
$w("#menuobsluga").hide();
$w("#klientgroup").hide();
}
else {
let firstRole = roles[0];
let roleName = firstRole.name;
//console.log(roles);
//console.log= (roleName); // "Role Name"
let roleDescription = firstRole.description; // "Role Description"

if (roleName === "obsluga" && user.loggedIn) {
$w("#menu").hide();
$w("#menuobsluga").show();
$w("#klientgroup").hide();
}
if (roleName === "Klient" && user.loggedIn) {
$w("#menu").hide();
$w("#klientgroup").show();
$w("#menuobsluga").hide();
}
else {
$w("#menu").show();
$w("#klientgroup").hide();
$w("#menuobsluga").hide();
})
}
}
}

@yisrael-wix

I edited it.

@katiejohnson7585 better… thank you

You wrote, “which doesn’t always help answer the question”. In fact, his replies always " help answer the question" as long as the user invests some effort as well. The forum is intended to be a place to get help, not to be spoonfed.