Showing certain data in a collection to specific members only

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

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.

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.
Velo: Working with Promises | Help Center | Wix.com

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.
Site Members: Managing Your Member Roles | Help Center | Wix.com
Site Members: Creating Member Roles | Help Center | Wix.com

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 Whiskey,

This looks fantastic, thanks for the reply.

I’m just noting that the code you attached references:

$w('#text13').show();

Does this mean that if the member has a certain member-role then the single object “text13” will display? My question is more focused around getting the whole contents of one or more items of the repeater (connected to the relevant data collection) to show only if a member has a certain role.

For example.

Let’s say the data in the collection is as follows:

Title - ID - Text - ( Which member-role I want to be able to view the data in the repeater)

  1. X - Y - Z - (MEMBER A)
  2. X - Y - Z - (MEMBER B)
  3. X - Y - Z - (MEMBER A)

Member A can see item 1 and 3 in the repeater, whereas member B can only see item 2.

Members who are both A and B could see both.

Is this possible?

@alex72569

Yes, if the member is that specific role then the text will be shown, you can change that to whatever element etc that you want.

Obviously, if you want to show or hide more elements depending on member roles, then you will need to modify the code from being single member role only to having the lines of code for if the member is another role etc.

Or have a look at using the code from the previous forum post and changing it to suit your own needs.