Hi All,
My website currently has a repeater that displays data items from a data collection.
The repeater is in a members-only section of the website.
I'm trying to figure out how to have particular items in the same data collection (and repeater) be visible for certain member-roles only. (Noting that some members will have multiple roles)
Any help would be greatly appreciated!
Please let me know if I can provide further info.
Kind Regards,
Alex
There is this previous forum post where you can take the code example and modify it to suit your needs.
https://www.wix.com/corvid/forum/community-discussion/set-member-role-using-wix-code
Also, you can try something like this for example.
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 === "MemberRoleName"){ $w('#text13').show(); } );
The getRoles() function is an asynchronous one, that means it doesn't return a value right away. That is also why it has a .then(). The code in the .then() only runs after the Promise returned by getRoles() resolves to a value. (The code where you check if the roleName is "MemberRoleName" actually runs before the code where you set the value of roleName.)
See here for Wix Support page about working with Promises in your code.
https://support.wix.com/en/article/corvid-working-with-promises
Alternatively, you can use async/await to deal with the asynchronous code.
Also, note that all site members will be classed as Member unless you define their actual role yourself.
https://support.wix.com/en/article/managing-your-member-roles
https://support.wix.com/en/article/creating-member-roles-6943237
Finally, note that with the code example above, you are only checking the first role of each user, so where you mention that the user has more than the one role, we can't be sure that the first role will always be the one that you are looking for.
Hi Heath! The page itself will be for all members. It's the data that appears in the repeater that I'm trying to restrict to certain member-roles.