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
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
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.
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(); } } }) });
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.
https://www.wix.com/corvid/forum/community-discussion/use-role-not-accessible-on-code
https://www.wix.com/corvid/forum/community-discussion/bug-wix-users-role-vs-getroles
https://www.wix.com/corvid/forum/community-discussion/get-the-information-of-user-roles
https://www.wix.com/corvid/forum/community-discussion/set-member-role-using-wix-code
https://www.wix.com/corvid/forum/community-discussion/user-roles
https://www.wix.com/corvid/forum/community-discussion/urgent-send-a-user-to-location-based-on-member-role