Slow multiple if-else run

I have a long code with if-else statements (28 cycles) which working too slow (4-5 sec).
Does anybody know any solution to get this part quicker?

My goal is to send my member to the right url about his/her category.
My site is about requests and vendors and there are 26 category (car-rental, catering, makeup, etc.). For that I create 26 passive site with filtered repeaters of the requests about the category.
When a member register input their category and after that I want to show them only the filtered requests about their category.

My code is (first 2 cycle is about the permissions >> registered and categorized member only, and the rest 26 is the investigations for the categories):

export function button8_click(event, $w) {
wixData.query(“Members”)
.eq(“_id”, wixUsers.currentUser.id)
.isEmpty(“membercategory”)
.count()
.then( (res1) => {
if (res1 === 1) {
wixWindow.openLightbox(“warningcategory”);}
else {
wixData.query(“Members”)
.eq(“_id”, wixUsers.currentUser.id)
.isNotEmpty(“membercategory”)
.between(“validityday”, 1, 30)
.count()
.then( (res2) => {
if (res2 === 0) {
wixWindow.openLightbox(“warningactivate”);}
else {
wixData.query(“Members”)
.eq(“_id”, wixUsers.currentUser.id)
.eq(“membercategory”, “Band”)
.count()
.then( (res3) => {
if (res3 === 1) {
wixLocation.to(“/requestsband”);}
else {
wixData.query(“Members”)
.eq(“_id”, wixUsers.currentUser.id)
.eq(“membercategory”, “CarRental”)
.count()
.then( (res4) => {
if (res4 === 1) {
wixLocation.to(“/requestscarrental”);}
else {
wixData.query(“Members”)
.eq(“_id”, wixUsers.currentUser.id)
.eq(“membercategory”, “Catering”)
.count()
.then( (res5) => {
if (res5 === 1) {
wixLocation.to(“/requestscatering”);}
else {
…
…
…

Thanx so much!
Imre

Hello,

From what i understood you first want to check the permissions, then redirect to the member’s category page.

To do that you don’t need that much of if statements and queries, you can first check if the member is registered and categorized, and then based on this single if statement, you redirect the member to the category page or open the lightBox.

This code can help you achieve the wanted functionalities:

export function button8_click(event, $w) {
  wixData.query("Members")
      .eq("_id", wixUsers.currentUser.id)
      .then( (res ) => {
         let result = res.items;
         if (result[0].membercatergory.length > 0 && result[0].validity >1 && result[0].validity < 30){
              let link = "/" + result[0].membercategory ; 
              wixLocation.to(link); 
         }
         else if(!(result[0].membercatergory.length > 0 )){
              wixWindow.openLightbox("warningcategory");
         }
         else if(!(result[0].validity >1 && result[0].validity < 30 ))
              wixWindow.openLightbox("warningactivate") ;               
         }
   });

hope it goes well !
Massa

Thank you Massa, unfortunatelly it is not working for me :frowning:
I am trying several variations of your logic but nothing.
My last probe was the simpliest but still nothing:

export function button8_click(event, $w)
{
wixData.query(“Members”)
.eq(“_id”, wixUsers.currentUser.id)
.then( (res ) => {
let result = res.items;
let link = ‘/requests’ + result[0].membercategory;
wixLocation.to(link);

  });             
  } 

I do not what’s wrong.

Imre

hello Imre

after .eq( ) there should be .find( ) then the rest of the code, so the code i suggested before will look like this:

export function button8_click(event, $w) {   
    wixData.query("Members")       
     .eq("_id", wixUsers.currentUser.id)  
     .find()    
     .then( (res ) => {          
        let result = res.items;         
        if (result[0].membercatergory.length > 0 && result[0].validity >1 && result[0].validity < 30){              
            let link = "/" + result[0].membercategory ;                
            wixLocation.to(link);          
         }         
         else if(!(result[0].membercatergory.length > 0 )){ 
              wixWindow.openLightbox("warningcategory");
          }         
         else if(!(result[0].validity >1 && result[0].validity < 30 ))             
               wixWindow.openLightbox("warningactivate") ;                  
          } 
    });  
}

Massa :slight_smile:

Oh, thx it is working well!
One more little thing: Is it possible to do a “not equal” statement in the multiple “if” investigation?
My goal is to check is the membercategory not equal to a category (eg. result[0].membercatergory <> “carrental” && …
but it seems ‘<>’ not working in this environment.

Thx a lot!
Imre

Hello Imre,
yes it is possible to use not equal expression, it would look like:

if (result[0].membercatergory !== "carrental" &&  result[0].membercatergory !== ...) 

however, you can use filters as to get all the categories that are not equal to the member category, and that’s as follows:

 import wixData from 'wix-data'; 
 
  wixData.query(" Members ") 
  .ne("category ",  result[0].membercategory ) 
  .find() 
  .then( (result) => {
  // result is the categories that are not equal to members category  
  } ) 
  .catch( (error) => { 
  let errorMsg = error.message; 
  let code = error.code; 
  } ); 

Massa

Thx it seems work! :slight_smile:

1 Like