Filter works on Preview but not on Live

Hi, please assist me. I have a gallery with filter buttons. The filters works fine in preview but on live it doesn’t work. I am able to upload data on Live but the filters are not working. The permissions are set to everyone and I did toggle sanbox. Any ideas? Below is the filter code:

export function dropdown1_change ( event ) {
let user = wixUsers.currentUser ;
let userEmail ;
user . getEmail ()
. then (( email ) => {
userEmail = email ;
});

**if** ( $w ( "#dropdown1" ). value  ===  "all" ){ 
    $w ( "#dynamicDataset" ). setFilter ( wixData . filter () 
    . contains ( "username" ,  userEmail ) 
    . contains ( "title" ,  "" ) 
    . between ( "date" ,  $w ( "#datePicker1" ). value ,  $w ( "#datePicker2" ). value ) 
    . between ( "description" ,  parseFloat($w ( "#input1" ). value ),  parseFloat($w ( "#input2" ). value )) 
    ); 
} 
**else** { 
    $w ( "#dynamicDataset" ). setFilter ( wixData . filter () 
    . contains ( "title" ,  $w ( "#dropdown1" ). value ) 
    . contains ( "username" ,  userEmail ) 
    . between ( "date" ,  $w ( "#datePicker1" ). value ,  $w ( "#datePicker2" ). value ) 
    . between ( "description" ,  parseFloat($w ( "#input1" ). value ),  parseFloat($w ( "#input2" ). value )) 
    ); 
}    

}

I’m moving this post to the Coding with Velo category.

First of all, wix-users has been deprecated and it is recommended to use wix-members instead.

Note: The APIs in wix-members and wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

Now regarding your problem… The most likely reason this isn’t working is that your filtering code is in the wrong place. The userEmail variable won’t be available since the filtering code will run before the Promise returned by user.getEmail() is resolved. You will need to move your filtering code to be inside of the .then() .

See the following for more information about Promises:

Thanks for your response. It’s still not working for all the filters. i.e. date and description. Can you please give me an example or fix the above code such that the filters work. Thank you in advance.

If it’s working in Preview, then I don’t see any reason that it shouldn’t work in Live. The only issue might be the permission settings of the database collection and the dataset.

A few comments…

When the dropdown value is “all”, you should just leave this out:

 .contains("title", "")

Seems as if that would check to see if title contains an empty string. Not sure what results from that.

Use console.log() statements to check the datePicker values. Then, see if the date field in your collection has values that fall between these two dates.

You’re converting two input fields to floating point numbers, and then checking if the value of the description field falls between these two numbers. Is the description field numeric? What’s in the description field? To me, “description” sounds like a text field. If the description field isn’t numeric, then this filter condition certainly won’t work.

Hi Yisrael

The permission is customized to Anyone I don’t think that’s the issue with permissions.
I did leave out the empty string filter which is not an issue as well.
datePicker does have values in the collection.
description is numeric and it works fine in preview.

It is very weird, eveything wowrks perfect and as expected in Preview but when I publish the site the filters are not working at all including in the onReady function.

What else can I try? Any suggestions?

What are you doing in the onReady()?

Do you have valid data in the Live collection? Do you see it there?

Yes, I do have valid data in the Live collection and I do see the data, but the filters are not working. Please ignore the onReady() function I’m not doing anything there.

The only other thing that I can think of is that in Preview, you are (automatically) the user. Therefore the filter would be with your email, and the permissions would all be admin/owner. In Live, someone has to log in and be the current user, and that would be part of the filter.

This is very strange. Even when I don’t filter by username, still the filter in Live don’t work. Permissions on the collection is fine, the filters on Preview are working fine. Here’s the code:

export function dropdown1_change ( event ) {

**if** ( $w ( "#dropdown1" ). value  ===  "all" ){ 
    $w ( "#dynamicDataset" ). setFilter ( wixData . filter () 
    . between ( "date" ,  $w ( "#datePicker1" ). value ,  $w ( "#datePicker2" ). value ) 
    . between ( "description" ,  parseFloat($w ( "#input1" ). value ),  parseFloat($w ( "#input2" ). value )) 
    ); 
} 
**else** { 
    $w ( "#dynamicDataset" ). setFilter ( wixData . filter () 
    . contains ( "title" ,  $w ( "#dropdown1" ). value ) 
    . between ( "date" ,  $w ( "#datePicker1" ). value ,  $w ( "#datePicker2" ). value ) 
    . between ( "description" ,  parseFloat($w ( "#input1" ). value ),  parseFloat($w ( "#input2" ). value )) 
    ); 
}    

}