How to filter a Multi-reference field?

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' ); 

$w ( “#datGroups” ). setFilter ( wixData . filter ()
. hasSome ( “memberCollection” , JSON . stringify ( res . items , undefined , ’ ’ )))
// )
. then ( () => {
console.log(“filtered!”);
} );

The filter is not working. How do I do it?

I’m not sure I understand. Let me see if I got it:

Collection: GROUPS
Fields:

  1. _id: a random id for each group record.

  2. memberCollection: a multi-ref field pointing to _id’s in the PrviateMembersData

Collection: datGroups
Fields:

  1. _id: random id string

  2. memberCollection: a multi-ref field pointing to _id’s in the PrviateMembersData

Is that right? Or maybe it’s something else?
Now, what you’re trying to filter?

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

Hope that’s clearer.

@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?

@jonatandor35 - datGroups is a dataset pointing to the GROUPS Collection.

  • The GROUPS Collection has the multi-reference field pointing to PrivateMembersData Collection.
  • A user logs in and I need to filter the datGroups dataset to only show the groups this user is in

@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);
})
}

Thank you! :slight_smile: