How to filter dataset for logged in user?

Hello

I’m creating a website for my hockey team and as part of the site we would like a members area where the user can update personal information, view documents etc.

We also want a section where players can see what they owe for membership fees.

Currently I have the private members data database (created automatically when adding a members area to the site) and then I have a 2nd collection named MembershipFees, this will be manually updated by the treasurer.

I would like to create a members page that shows the logged in user their remaining balance which is linked to the 2nd collection called MembershipFees. As the logged in user won’t own the data in the MembershipFees collection I haven’t been able to filter it that way.

Please could anyone assist me?

Many thanks

1 Like

I’m also trying to figure this out.
I am trying to set up image galleries that only display to a specific user showing off behind the scenes photos of their specific product order or PDFs relevant to them specifically

First way, you can use dynamic dataset APIs to create this logic. Inside frontend.
Frontend Code:

import { currentMember } from 'wix-members';
import wixData from 'wix-data';

$w.onReady(async function () {
    const { _id } = await currentMember.getMember();
    $w('#dynamicDataset').setFilter(wixData.filter().eq('member', _id))
});

Second way, you can create backend function and use it to get filtered items.
Backend Code:

import wixData from 'wix-data';
import { currentMember } from 'wix-members-backend';

export async function getMemberItem() {
    const { _id } = await currentMember.getMember();

    return wixData.query("TestDatabase")
        .eq("_owner", _id) //or member
        .find()
        .then((queryRes) => {
            const { items } = queryRes;

            if (items.length > 0) {
                return items;
            }
        })
}

Frontend Code:

import { getMemberItem } from 'backend/aModule.jsw';

$w.onReady(async function () {
    const items = await getMemberItem(); //items you get from database as filtered to current member
});

Here is the video with details: Filtering Database based on Logged-in Member | Claap

If you have questions let me know. And I hope you can understand my English :slight_smile:

APIs used here:
setFilter - Velo API Reference - Wix.com
query - Velo API Reference - Wix.com
getMember - Velo API Reference - Wix.com
getMember - Velo API Reference - Wix.com

1 Like

Thank you so much for your detailed response, the video was very informative as well.
I really appreciate the time you took on this!

If you have any tutorials or more Wix videos I’d love to keep learning!

Thanks again

No problem!

I’m planning to create Wix videos soon when I start to post I will link my YouTube channel under here.

And if the answer was helpful and if you were able to solve your problem, can you pick my answer as best answer from the options of my answer.

I have just been making other changes to the site and for some reason the code you gave me is now only returning the newest entry into the database, regardless of the member associated with it. So every member is now seeing the same.

I’ve followed your instructions to the letter, when I run the getMemberItem() function the output is correct, but the front end continues to show the wrong thing :thinking:

Please see the image below:

Can you give me access to the website so I can take a look and tell you what the reason was.

Done

Sorry I was late but here is the problem.

We don’t have any problem with backend or frontend code. In fact, we don’t have any problem. Text elements inside the page are connected to dataset but but we are doing with backend function is not related to dataset any we are not changing anything with dataset data. Because of that you were still seeing first item from the database.

What you need to do is you need to pass the current member data to text elements using code not using dataset feature.

First Way (Using $w.Text HTML )

You can pass HTML code to text using .html property of $w.Text elements. And this is the one of the ways you can achieve what you want. In your page you have at least two Text for title and for the data but actually you don’t need that much element when you use HTML.

Here is the code with HTML:


import { getMemberItem } from 'backend/memberFilter.jsw';

$w.onReady(async function () {
    const membershipData = await getMemberItem();

    //One Text Element using HTML -------------
    $w('#matchFees').html = `<b><p style="font-size: 18px">Match Fees:</b> £${membershipData.matchFees}</p>`;
    $w('#clubFees').html = `<b><p style="font-size: 18px">Club Fees:</b> £${membershipData.clubFees}</p>`;
    $w('#membershipType').html = `<b><p style="font-size: 18px">Membership Type:</b> ${membershipData.membershipType}</p>`;
});

Second Way ($w.Text.text)

When we use text property, we can’t make title bold so because of that you will need to use two elements. One for title one for data.

Here is the code with text:

import { getMemberItem } from 'backend/memberFilter.jsw';

$w.onReady(async function () {
    const membershipData = await getMemberItem();

    //Two Text Element Using Text -------------
    $w('#matchFees').text = `£${membershipData.matchFees}`;
    $w('#clubFees').text = `£${membershipData.clubFees}`;
    $w('#membershipType').text = `${membershipData.membershipType}`;

});

Third Way (Using Dataset APIs)

If you want to keep using dataset and connect data using data connection. Shortly if you don’t want to write code to pass data to elements here is your way. You will use your current setup and you will only need two lines of code.

Here is the code with Dataset Filter API:

//Velo API Imports
import wixData from 'wix-data';
import { currentMember } from 'wix-members';

$w.onReady(async function () {
    // Three text element using dataset -------------
    const { _id } = await currentMember.getMember();
    $w('#dataset1').setFilter(wixData.filter().eq("member", _id));
});

*Note: If you use backend function and connect data using text or html property of Text elements. You should do the same for any extra element that you will add to the page.

Here is the video: Filtering with Current User | Claap

I can’t thank you enough, I’ve finally got it working as expected.

You’ve been a really great help! Thank you so so so so much! :grin: