I have a collection called GROUPS, which has a multi-reference field (‘memberCollection’) to the ‘PrivateMembersData’ collection. I need to filter the collection based on the currently logged on member. If he’s in the group, then I need to visualize a repeater connected to the GROUPS collection.
Here’s the code:
**let** res = **await** wixData . queryReferenced ( 'GROUPS' ,
wixUsers . currentUser . id , 'memberCollection' );
datGroups is a dataset which should filter the repeater I have on my page. The repeater should display ONLY the groups where the currently logged on member is part of. Therefore,
Dataset: datGroups
points to GROUPS
filters records where user is in membersCollection
@internationalcovidsu so to make it clear:
datGroups has a field group wich is a single reference field pointing to GROUPS?
while the GROPUS collection has a muliti-reference field ( membersCollection ) pointing to PrivateMembersData collection?
And you want to filter the datGroups to only display items that pointing to a group that refers to the current member?
@internationalcovidsu ok. got you.
If it was a regular array with member id’s it could be easy as all you had to use is .filter().contains(‘memberCollection’,userId);
But since you used multi-reference field, you’ll have to run an additional query.
Something like:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(() => {
$w("#datGroups").onReady(() => {
filterDataset();
})
})
function filterDataset(){
return wixData.queryReferenced('Members/PrivateMembersData', wixUsers.currentUser.id, 'groups');//instead of 'groups' use the auto-generated corresponding field as appeared in PrivateMembersData
.then(({items}) => {
return $w("#datGroups").setFilter(wixData.filter().hasSome('_id', items.map(e => e._id));
})
.then(() => console.log('filtered')).catch(err => err);
})
}