Filtering a Dataset by User (NOT Owner)

Hi all,

I’m having a little trouble with something that I’m trying to do.

I currently have a database containing a list of vehicles owned by my site’s members. I will be managing and updating the data personally, so all records have my user id in the Owner field.

I would like to have a list in the members area of my site which shows a user all of THEIR vehicles, however because I am the owner of all the records I can’t simply filter by Logged in User on the Owner column.

My solution to this was to have an email address column in the database and filter by that, however my attempts at fetching the currently logged in user’s email all failed - I’m not sure if I understand how to use the APIs. If somebody could please explain the process to get the user’s email address and then use that address to filter a dataset, that would be extremely helpful.

Alternatively, is there another way that I could go about filtering these results? I’m open to any ideas.

Thanks very much,

Scott

Hi Scott,

This how the function getEmail() suppose to be:


import wixUsers from 'wix-users';

wixUsers.currentUser.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

I suggest you to add this method to the onReady() function of the member area page. This function runs before the user starts interacting with your page.

If you still have a problem, please send us a URL to your site and specify the name of the page.

Best,
Sapir

Hi Sapir, thanks very much for your reply. I swear that I had tried that exact code previously and kept getting an error about an unhandled promise in the console, but when I typed it in this time it seemed to work fine.

Now that I think I have the email address, I’m trying to filter my dataset with the code below, but that doesn’t seem to be working. clearly I’m missing something.

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( function () {
//TODO: write your page related code here…
wixUsers.currentUser.getEmail()
.then( (email) => {
let userEmail = email; //"user@something.com"
})

$w("#myVehiclesSet").setFilter(wixData.filter() 
    .eq("vehicle_user", wixUsers.userEmail)) 

});

Hi Sapir,

Please feel free to ignore my previous post, as I figured out where I was going wrong. I solved my problem and now have the dataset filtering correctly with the code below:

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

$w.onReady(function () {

 //Get current user email address
    wixUsers.currentUser.getEmail()
        .then( (email) => {
 let userEmail = email;  //"user@something.com"
 
 //Filter the dataset
            $w("#myVehiclesSet").setFilter(wixData.filter()
                .eq("vehicle_user", email))
        })

});

Again, thank you SO much for your assistance.

Hi
I tried this and it does not work for https://suriyast.wixsite.com/grant-gem
on the free members page -
Can you help?

Hi Uriya,

In the dataset properties there is an option to set a filter according to one of the fields.
https://support.wix.com/en/article/filtering-database-content-displayed-on-your-page

Best ,
Sapir

Hi
Thanks
This does not help
Like Scoot I need Filtering a Dataset by User (NOT Owner or other ui accisable one)

Tried Scott’s code but it dòes not work …

Hi Uriya

Add an onReady() function to your dataset and inside the handler add the setFilter() function:

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

$w.onReady(async function () {
 let userEmail = await wixUsers.currentUser.getEmail()
    $w("#dataset1").onReady(() => {
        $w("#dataset1").setFilter(wixData.filter()
            .eq("User email", userEmail)
        )
    });

});

Best,
Sapir

Hi Sapir,
Tried to copy your code,
Did not work, getting no results
see screen shots

Hi,

You need to change the parameters according to your database:
In the line below the “User email” parameter need to be the field key in your database.

 .eq("User email", userEmail)

Best,
Sapir

@jeff-haskins

Just input the email result into the input element.
*In the code below the id of the input element is “input1”

wixUsers.currentUser.getEmail()
        .then( (email) => {
           $('#input1').value= email;
  });

Because this function is inside the onReady() function after the page is loaded the input element in the form will be filled in the current user’s email , so now he will only have to fill the rest of the inputs.

Best,
Sapir

BUMP Any update with this i have a similar issue and currently have the following code but does not return the right values from the database just the first row of values…

(email addresses in my database are under the column ‘Client’)

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( function () {

//Get current user email address
wixUsers.currentUser.getEmail()
.then( (email) => {
let userEmail = email; //"user@something.com"

//Filter the dataset
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“Client”, email))
})

});

@sapirh could i use this code for multiple values from the current user based on ID? For example:

wixUsers.currentUser.id()
        .then( (email) => {
           $('#input1').value= email;
           
            .then( (list2) => {
              $('#input2').value= list2; 
              
               .then( (list3) => {
                 $('#input3').value= list3; 
  });

In this code the dataset would have email, list2, and list3.

Sapir, this worked great for me to filter by email…THANK YOU! However, I now want to filter data to the Company associated with the currently logged in user. I do ask for Company on my custom member sign up form. However, I don’t see the Company field from the custom form in my Private Members Data collection, so I can’t figure out how to filter on that field. Can you help?

Sapir…can you filter by domain address, i.e. anything @companyname.com?

Hi! I have code that is doing a good job of matching the email address of the currently logged in user to the “submittedByEmail” in my collection. However, I would prefer to filter by the Domain of the currently logged in user, which I have as a column in my dataset as “submittedByDomain”. Do you how I would alter the code so that it shows the data for the currently logged in user that matches the “submittedByDomain” of that user in my dataset?

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( async function () {
let userEmail = await wixUsers.currentUser.getEmail()
$w(“#dataset1”).onReady(() => {
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“submittedByEMail”, userEmail)
)
});

});

Most likely the “Client” does not refer to the id of the column in data go to data column and click on setting

I’m essentially doing a similar thing here but running into issues.

I need to create some sort of query based on the currentUser and run it against a second dataset on the page.

I need to write the email (currentUser) and the first name and last name from the first data set (members) to the second dataset if they don’t already exist there and in page’s form allow the user to edit their other details in the second data set. Sapir or others is there a way you could assist me?