Dynamic filtering of repeater

Hi All, got an issue which I’m stunted by.
Im building a dog based forum, and want users to be able to upload a “sub-profile” for each of their dogs to then be displayed next to their profile.

While it’s been relatively easy setting up a custom form, connected to a dataset and showing the content the owner has uploaded. I have not been able to find a solution for a user seeing another users profile and displaying that users dogs. (i’m currently using a repeater that I thought I would be able to filter relatively easily)

Hence very similar to forum posts but for these “dog” profiles. Could anyone either direct me to a forum post describing a similar problem or have any ideas how to solve this? im currently unable to find a post that solves a similar issue (probably googling after the wrong term)

I presume I need to extract a user ID from the CRM and filter the repeater using this?

Best Regards
Max

Hello Max,

you should perhaps also mention what is the situation when looking at your elements and databases?

  1. How is structured your database?

So if you have a “sub-profile” it will sure be connected to a database, where you store all the information to each dog of each user (dogs-profile). Let us call this database “dogs-profile”.

So, every time when a user creates a dogs-profile, you will have to safe the user-id of the dog-owner. For this, you will need to get the current-users-id, to save it into your “dogs-profile” database.

With this code you can get the current user-informations.

import wixUsers from 'wix-users';
2
3// ...
4
5let user = wixUsers.currentUser;
6
7let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
8let isLoggedIn = user.loggedIn; // true
9
10user.getEmail()
11  .then( (email) => {
12    let userEmail = email;      // "user@something.com"
13  } );
14
15user.getRoles()
16  .then( (roles) => {
17    let firstRole = roles[0];
18    let roleName = firstRole.name;                // "Role Name"
19    let roleDescription = firstRole.description;  // "Role Description"
20  } );
21
22user.getPricingPlans()
23  .then( (pricingPlans) => {
24    let firstPlan = pricingPlans[0];
25    let planName = firstPlan.name;          // "Gold"
26    let startDate = firstPlan.startDate;    // Wed Aug 29 2018 09:39:41 GMT-0500 (Eastern Standard Time)
27    let expiryDate = firstPlan.expiryDate;  // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
28  } );

Till here no problem, you simply get the ID and safe it to the dogs-profile-database.

Your Problem will START, when you want to show another users “dog-profile”.
And YES, exactly this is the case. You simply won’t be able to show the informations of other users (using front-end).

And here come the TRICK! We use the back-door :grin:.

import wixUsersBackend from 'wix-users-backend';
2
3// ...
4
5let user = wixUsersBackend.currentUser;
6
7let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
8let isLoggedIn = user.loggedIn; // true
9
10user.getEmail()
11  .then( (email) => {
12    let userEmail = email;      // "user@something.com"
13  } );
14
15user.getRoles()
16  .then( (roles) => {
17    let firstRole = roles[0];
18    let roleName = firstRole.name;                // "Role Name"
19    let roleDescription = firstRole.description;  // "Role Description"
20  } );

You will have to use the back-end-coding, to achieve your aim!

With this UPGRADE-CODE you will be able to find what you need.

Hi Dima,
Thank you for the input. And sorry for the crap description.
I’m unclear if this will solve the issue since it seems like the backend function will give me the wixUsersBackend . currentUser according to the documentation (that I used to research this), I’m very new to Corvid hence apologies if I’m missing something significant here.

For Clarity:
My current data schema, I’m using the owner- tag in the database to tag every dog input (as per defined by the default owner field (which I assume is the user ID). All information per dog is kept in one row in the DB.

Hence if i’m user 1 (Max, logged in). I would then want to filter the repeater based on User 2 (Muge in the profile card) and see the dogs she is the “owner” of. I would assume this would be as simple as applying a filter for the profile being viewed on the Dataset, though that seems to not be the case.

As per your description it seems like I’m still receiving my own user ID (hence User ID for User 1 rather than User 2)?

import wixUsersBackend from 'wix-users-backend';
2
3// ...
4
5let user = wixUsersBackend. currentUser;
6
7let userId = user. id;     

So I should then be able to filter based on this userId = db.owner ?

Hacking and slashing a bit I tried the below:

import wixUsersBackend from 'wix-users-backend';
import wixData from 'wix-data';
let user = wixUsersBackend. currentUser;

let userId = user. id;    


$w. onReady(function () {
 // TODO: write your page related code here...


let myFilter = wixData. filter().eq('owner', userId)

$w('#dataset1'). setFilter(myFilter)



});

Which is returning an error, not sure if the above description gives a bit more clarity on what I’m trying to achieve?
Sorry about the crap descriptions, incredibly new to both Javascript and Corvid.

I’m surprised this functionality does not exist in the editor tbh :slight_smile:

Grateful of any help on this, even if its just a google search term to the right resource to read up on it :slight_smile:

Best Regards
Max

@max5626
Sorry i gave you the wrong back-door :sweat_smile:

This is the right one…

import wixUsersBackend from 'wix-users-backend';
2
3export function getUser(id) {
4  return wixUsersBackend.getUser(id);
5}
6
7/* Returns a promise that resolves to:
8 * 
9 * {  
10 *   "id": "dn8sf9c2-4e9f-a02d-a58d-f244d999729a",
11 *   "memberName": "John Doe",
12 *   "firstName": "John",
13 *   "lastName": "Doe",
14 *   "nickname": "johnd",
15 *   "slug": "johnd123",
16 *   "language": "en",
17 *   "status": "ACTIVE",
18 *   "loginEmail": "john.doe@somedomain.com",
19 *   "creationDate": "2019-08-05T11:29:39Z",
20 *   "lastUpdateDate": "2019-08-12T12:29:43.810Z",
21 *   "lastLoginDate": "2019-08-12T13:42:30Z",
22 *   "emails": [
23 *     "john.doe@somedomain.com",
24 *     "doughyjohn@anotherdomain.com"
25 *   ],
26 *   "phones": [
27 *     "5555555555",
28 *     "5555555556"
29 *   ],
30 *   "labels": [
31 *     "contacts-new",
32 *     "contacts-site_members_approved"
33 *   ],
34 *   "picture": {
35 *     "url": "https://.../photo.jpg"
36 *   }
37 *   "customText": "Custom Text",
38 *   "customNumber": 12345
39 * }
40 */

When you are working with backdoor-coding, then you will need to write code in two sections.

  1. FRONT-END
  2. AND IN BACK-END

So you will have a back-endfile called as JSW or JWS or something like this.

I would suggest you first to understand the difference between front-end and back-end and how to code in both situations.

The code given above is the back-end-code which will be stored in your backend-module.

Then you will need to write a front-end-code for this to start/call the back-end function.

So at the end, you will need 2-CODES (front-end-code-part and back-end code-part).

Amazing, will hack and slash a bit more and see what i copy paste together :slight_smile: thank you so much! :slight_smile:
Best Regards
Max

Hi Dima, So I tried your example, and it seems like its returning the Id of each element in the repeater rather than of the profile im trying to capture

Running the following on backend:

// Filename: backend/filter. jsw (web modules need to have a .jsw extension)
import wixUsersBackend from 'wix-users-backend';

export function getUser(id) {
 return wixUsersBackend. getUser(id);
}


Running the below on frontend:

import {getUser} from 'backend/Filter'
import wixData from 'wix-data';
let userId = getUser. id;
$w. onReady(function () {


$w("#dataset1"). setFilter(wixData. filter(). eq("_owner", userId));

});


Using the same filtering on another page with current user that works perfectly, so pretty sure the filter works.

When I output the userId into a text box it returns a null value, hence I suspect I’m getting a null value from the backend function. Though I would think that the filter would prevent anything from showing rather than showing everything?

I further published the site just to be sure that the backend function was running as well.

Wondering if it could be that the function does not know which user ID to get?

Best Regards
Max

You may take a look at my “Comment/Chat-Box-EXAMPLE” in my collection.
It could help you to solve your problem. I had the same problems few weeks ago :grin:
Read the forum post related to the mentioned example.

The example you will find here…

https://russian-dima.wixsite.com/meinewebsite

Heya Dima, tried to check the code for the example, so if I understand this I literally need to create a new non private members DB that links the different dogs to the owners and then extract the owner ID from there?

Take a look at your DB(database).
Yes that was my idea.
Did you want to solve it in another way?

You can take the user_Id (owner_ID) out of the “PrivateMembersData” (right now by using the BACK-DOOR :grin:).

And this USER-ID is all you need from the “PrivateMembersData” (on front-end).
(Putting this ID to back-end, to get the right user-data out of the protected PMD)

All the other steps will happen in your “PET-DATABASE” :rofl:

  1. User loggs in —> “Max”
  2. CurrentUserID will be loaded
  3. You put this USER-ID to BACK-END to get the DATA of the CURRENT-USER.
  4. Now you have the data of the CURRENT-USER. (every-user-possible)
  5. Next step is to filter all the DOG-PROFILES by this ID in your “PET-DATABASE”
  6. What do you have now?

RIGHT!

  1. All data of the USER
  2. Filtered-DATA of all PET-PROFILES belonging to this USER.

IMPORTANT values in this case are —> USER-ID and OWNER-ID (perhaps e-Mail)

Heya Dima, so this would by your logic return the “user data” of the current user.

The logic I need to figure out is:

  1. User 1 (max) logs in,
  2. views another profile on the platform, user 2,
  3. Retrieve user 2’s ID which is displayed in the member profile as this:

4: apply this user ID to filter the dogs.

hence the problem seem to be to access this member profile box, even though i’m able to withdraw the user IDs, i’m finding no way to define which user i’m currently viewing. That is if its user 1, 2 or 3 that is currently showing in the card. I assume this is because of some security feature

Seems like the method you describe above would essentially recreate a current user filter?

Best Regards
Max


Looking at the log of the page seems like its trying to call the filter with an empty array.

I have first to look at it myself :grin: (i will do it tomorrow).