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
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
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