Hey, hope everyone is doing good. I need some assistance with my if-else statement. For reference, I attached a photo so you can see, visually, what I’m talking about.
Anyway using Velo’s code and API, I tried to make it so if the database value belonging to the “status” item is empty then the four text assets (early access, public release, etc) will be visible BUT the two buttons (Download and LSPDFR Alt. Link) isn’t. If the value belonging to the “status” item isn’t empty regardless of what the value is, then the reverse will happen (buttons are visible and the four text assets are hidden).
But for some reason what is happening is that the four text assets are visible and the two buttons are hidden regardless if the value of the “status” item is empty or not. Please any help I can get I’ll gladly take. My website deadline is arriving soon and this is an issue that I didn’t think would happen. Please, if you know any tips or something I did wrong, let me know!
Here’s my code:
import wixData from 'wix-data';
wixData.query("Downloads")
.isEmpty("status")
.find()
.then( (results) => {
if(results.items.length > 0) {
// If the product is unreleased.
$w("#text14").show();
$w("#text35").show();
$w("#text36").show();
$w("#text37").show();
$w("#button2").hide();
$w("#button3").hide();
} else {
// If the product is released.
$w("#button2").show();
$w("#button3").show();
$w("#text14").hide();
$w("#text35").hide();
$w("#text36").hide();
$w("#text37").hide();
}
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
the If Else statement is fine. It is written well and placed in the right spot. The issue; I think, is with your query. You are currently looking through the entire downloads database and returning all objects whos status is undefined. Not filtering by a specific item. This should return an object as results.items. Calling .length on an object returns undefined which could cause it throw an error and not evaluate the if else statement. I would start by calling console.log() on the returned data to ensure it is not misshaped.
Thank you for the fast response although I am a bit confused on what you mean. What I took from your answer is that the .length portion is messing stuff up hence I should only include the results.items part. But I thought the results.items.length > 0 is what being compared. Like if there’s 0 characters that means it’s empty thus the statement will go through.
@lujerex just comment out the if else and console.log(response.items) If it is an object instead of an array calling .length on it will return undefined and throw an error in the console causing your if else not to evaluate. To fix this you will need to call Object.keys() on the returned object and then check it’s length.
@amotor Hey sorry for the late reply. Had a doctor’s appointment and it surprisingly took me out for the rest of the day. Thank you for the answer but could you explain in layman’s terms? I’m a beginner in Javascript and only know any TINY TINY bit of C# so I’m not used to all the terms and methods and how to use them.
Edit: Nvrm someone else helped me out. Thanks for the help you did given me amotor.
Noah Lowell from the Velo Discord server gave me this answer.
It works flawlessly. Just adding this so anyone else with my issue will see the answer if they happen to stumble upon this thread when searching for answers.