how to filter a table according to the current user

I would like to show only a few document releases according to the user you are accessing.
For example: Each user must have private access to a database documents through a table.
How do I collect current user information (email for example) and use it as an intruction for filtering?

2 Likes

Import your data for example use a Get after that make a filter and use the value youā€™ll use tofilter your data.

For example youā€™ll do something like this

import wixData from 'wix-data'; 
// ... Onready
 wixData.get("myCollection", "00001") 
 .then( (results) => { let item = results; 
 } )
 .catch( (err) => { let errorMsg = err; } );
 //Cath your value for exmple (let filter = D)
 
 $w("#myDataset").setFilter( wixData.filter() 
 .startsWith("lastName", "D") .ge("age", "21") );

Youā€™ll filter for names that start with D being your attribute D that you extracted from your GET

Hi Vinicius,
No code is required for this feature.
Check out this article of how to do it:
https://support.wix.com/en/article/how-to-filter-a-page-based-on-the-currently-logged-in-user
Good luck!
Roi.

1 Like

The link you sent me was very enlightening. But it is not yet. Because Iā€™m going to add the files, so when I perform this filter it will not work because Iā€™ll be filling out the form.

I was able to perform the filter according to the user logged in using the following code.
it is important to note that the email must be registered in the database in the correct way for the filter to work.

import wixData from 'wix-data';
import wixUsers from 'wix-users';

$w.onReady(function () {

 // ...

 let user = wixUsers.currentUser;

 let userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
 let isLoggedIn = user.loggedIn; // true
 let userRole = user.role; // "Member"

  user.getEmail()
    .then((email) => {
 let userEmail = email; // "user@something.com"
      console.log(userEmail);
      console.log(userId);

      $w("#dataset1").setFilter(wixData.filter()
        .eq("email", userEmail)
      );
    });

});

1 Like

Hi!
Iā€™ve been trying to use this method to have members shown a personalised list of events upon login.

Iā€™ve been using the last code youā€™ve pasted but iā€™m getting errors here -

$w(" #dataset 3").setFilter(wixData.filter()
.eq(ā€œemailā€, userEmail)

the #dataset3 section tells me Its not a valid section and ā€œemailā€ says itā€™s not valid either.

Any tips for where i could be going wrong?

Instead of putting dataset3 you should put the name of your dataset. If you need help with this let me know and we can make a skype. I can explain to you in 1 minute.

Thank you!, i worked out where i was going wrong in the end. I tried to place that code as a hook ON the database not on the page which is why it couldnā€™t see the assets.

Is there a way to make it so it does this process before it pulls in and places to speed it up?

Hey I am really struggling to get this to work for me. I have a table that I want to filter according to the user that is logged in to show private data. Where do I insert the above code?

1 Like

1: Enable Wix Corvid
2: Go to the page where the table or element you want to filter based on the user information is.
3. You should open the bottom flap by clicking the arrow.
4. Paste the code by tailoring the IDs according to what you have set up on your page.
5. Soon.

Note: I created this code to perform a filter on a table. Perhaps for other elements you need to change or add functions.

1 Like

No. This is not possible. It need to be done on the page, not in the database.

Thanks Viniciusā€¦ Ive followed that but I get the following errorsā€¦
Unhandled promise rejection TypeError: Failed to fetchFailed to load wix data hooks: Cannot find module 'wix-usersā€™Failed to load wix data hooks: Cannot find module ā€˜wix-usersā€™

Dear Vinicius, this was my first time using code and yours worked perfectly. Thank you so much