Sort table by user email

Hello I am trying to sort a table by the logged in users email address against a column in a collection.

What I have are teachers submitting evaluations for students. An email account can have multiple students, a student can have multiple evaluations. When someone logs in, I want them to only see their students evaluation results matched to the logged in user.

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

$w.onReady( function () {

$w(“#dataset1”).onReady( () => {
// get the current user’s email
wixUsers.currentUser.getEmail()
.then((email) => {
// set the filter to only show items where “email” field
//matches current user’s email
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“email”, email)
)
})

});
});

This doesn’t work the way I think it should.

When I filter by another column and manually set what it should be equal to, only shows that information.

Maybe I should be using query? Am I on the right path?

Hello Alex,

Filtering is the correct way to go in this situation, can you share some screen shots of the table and the expected output vs the actual output?

Best,
Majd

the table row should be a clickable element that goes to a page that displays the results of the evaluation for that particular student in that particular class

Hello Chris,

Try the code I have posted below, I narrowed it down to two possible errors:

  1. The email you are filtering is not the one that is in the collection. (I put a console.log to check the email)
  2. You are not consuming the promise that is returned by filtering the dataset. (using .then() after filtering)
import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
 
   $w("#dataset1").onReady( () => {
    wixUsers.currentUser.getEmail()
      .then((email) => {
         console.log(email, 'here is the email being sent to filter');
         $w("#dataset1").setFilter(wixData.filter()
          .eq("email", email)  
         )
         .then( () => {   
         console.log("Dataset is now filtered"); 
         })
         .catch( (err) => {   
         console.log(err); 
         });    
      });
  });
  
}); 

This code should solve those two problems, see the console log to see if the email is correct.

Best,
Majd

yes the email is correct. your code works. I am still getting this:

type mismatch of some kind? does it matter that this is a reference field and the key in the referenced collection is a different name of the key in the parent collection?

No it should not matter, can you try .hasSome instead of .eq and let me know how that goes?
hasSome

Same

You are using parentemail1@gmail.com correct? If that is the case the table filtered the results correctly and got the rows with that filter criteria

@mqumseya I have changed it to the email of the user I am currently logged in with but I am using that space in the database. It is not returning any result.

Would it be possible to send me your wix website URL so I can test it out?

the pages are currently not live on the site. columbusdancecda.com

also have to be a teacher or parent role.

notice I changed the code to match against the student name and hard coded the student name. it filtered correctly. when I try to filter by matching against the logged in user email. it fails.

Any luck? I still cannot make this work.

Hello Alex,

Try turning your email to a string before you compare it in the filter function like so:

.eq("email", email.toString())  

This will fix the type mismatch if there is any. Put this in your initial code that you posted the first time.

still nothing

Did you try this - https://support.wix.com/en/article/wix-code-how-to-filter-a-page-based-on-the-currently-logged-in-user