Filter dataset by Member ID matching a memberID field in the collection

Question:
How do I filter a dataset comparing the (logged in) Member ID to a field in the dataset?

Product:
Wix Studio

What are you trying to achieve:
I have a table called Students. Members (“mentors”) of the site can “assign” students to themselves by pushing a button which programmatically adds the Member ID of the mentor to a field called MentorID in the Students Table. I want Mentors to be able to see their mentees by going to a page which has a repeater that filters the Students Table to where the MentorID field matches the current Member ID.

What have you already tried:
I don’t see a way to do this in the Studio UX. If there is a different approach that allows me to do this without code (here and in the original assignment of a student to a Mentor), that is absolutely preferable.

I tried the following code:

function set_FilterByMember() {
	let options = {fieldsets: [ 'FULL' ]}
	currentMember.getMember(options)
	.then((member) => {
		const memberID = member._id;

		$w("#studentsDataset").onReady(() => {
			let myFilter = wixData.filter().eq('mentorId', memberID)
		$w('#studentsDataset').setFilter(myFilter)
		});

	})
	.catch((err) => {console.error(err); });

}

Additional information:
I’ve simplified this down so might have blown it in cleaning up the code. Hopefully you get the idea.

Your approach is on the right track, but there are a couple of adjustments needed in the code to ensure it works as expected.
Please call this function set_FilterByMember(); to onReady function
make sure the id of dataset needs to b correct… copy the mentorid from the database and paste into the code. test it and check this in the console
import wixData from ‘wix-data’;
import { currentMember } from ‘wix-members’;

function set_FilterByMember() {
// Fetch the currently logged-in member’s ID
let options = { fieldsets: [‘FULL’] };
currentMember.getMember(options)
.then((member) => {
const memberID = member._id; // Get the Member ID

        // Wait for the dataset to load
        $w("#studentsDataset").onReady(() => {
            // Create a filter based on the Member ID
            let myFilter = wixData.filter().eq('mentorId', memberID);
            
            // Apply the filter to the dataset
            $w("#studentsDataset").setFilter(myFilter)
                .then(() => {
                    console.log("Filter applied successfully");
                })
                .catch((err) => {
                    console.error("Error applying filter:", err);
                });
        });
    })
    .catch((err) => {
        console.error("Error fetching member data:", err);
    });

}

What? No.
Don’t wait for the database to load before setting a filter, now the site will load the data twice. Furthermore, it will then trigger the dataset’s onReady again, and filter again, resulting in an infinite loop

Set the filter immediately, the dataset will only load once with the coded filter

The only problem with OP’s code is they trigger a filter change every time the dataset loads, which triggers the dataset loading again, infinite loop

1 Like

Thanks for the feedback and ideas. Between the two responses, I was able to get things working.

My main issue was indeed trying to make a call before the Dataset was ready, although it was not in the code I gave you. :person_facepalming: But you both taught me something: watch and understand how and when things are loaded before using them.

Thanks again!