How can I check if a dataset field is empty?

Some fields are optional and the user may not have supplied values for them. How can I check in code whether a field is empty or not?

Thank you.

Fields that are empty usually have falsy values. So you can set an if statement to exclude the value if it is either undefined or ‘’.

falsy?..

If tried checking if it’s undefined, if it’s ‘’, “” … none of this works and I know the field is empty. I’ve tried it with different empty fields, nothing. This should be a real simple thing to do if wix has a NULL equivalent and a way to check it. Why this has to be confusing and bizarre is beyond me

if(!fieldKey){
//code
}

The way to check it is always console.log()

I know this is an old post but I need help with this as well and I haven’t been able to find a workable solution. All I need to do is check is a field is empty or not so can show / hide the label. I don’t need to match or validate.
Actually each row has 3 fields that need to be checked, as long as one of the three is not empty I want my label to show. Please help.
Thanks in advance

export function repeater1_itemReady ( $item , itemData , index ) {
$w ( “#repeater1” ). onItemReady ( ( $w , itemData , index ) => {
if ( itemData . station ! = “” ) {
$w ( “#btnShowHide” ). show ();
}
else {
$w ( “#btnShowHide” ). hide ();
}
});
}

Hey JC,

Say your fields are .station, .barge and .chocolate. You can use the following code to check if at least one of them is not empty. This will “short-circuit” if any one of those fields actually have a value, and will show your label.

export function repeater1_itemReady($item, itemData, index) {
$w("#repeater1").onItemReady( ($w, itemData, index) => {
 if( itemData.station || itemData.barge || itemData.chocolate ) {
        $w("#btnShowHide").show();
 }
 else { 
        $w("#btnShowHide").hide();
 }
});
}

Chris,
Thank you so much!! I’ve been struggling with this for a while, it’s part of a pet project to help me learn to get more out of Wix. Here is what I turned it into to meet my exact needs and it works great. Any efficiently tips are always welcome. Thanks you again.

Notes: I know that I don’t need to use collapse for show and hide, but the three fields are stacked under the label, this allows any field that has data to be tucked right under the label so there are no gaps.

export function featureListings_itemReady ( $item , itemData , index ) {
$w ( “#featureListings” ). onItemReady ( ( $w , itemData , index ) => {
if ( itemData . station1 || itemData . station2 || itemData . station3 ) {
$w ( “#txtBtsMrt” ). show ();
}
else {
$w ( “#txtBtsMrt” ). hide ();
}

if ( itemData . station1 ) {
$w ( “#transit1” ). expand ();
$w ( “#transit1” ). show ();
}
else {
$w ( “#transit1” ). hide ();
}

if ( itemData . station2 ) {
$w ( “#transit2” ). expand ();
$w ( “#transit2” ). show ();
}
else {
$w ( “#transit2” ). hide ();
}

if ( itemData . station3 ) {
$w ( “#transit3” ). expand ();
$w ( “#transit3” ). show ();
}
else {
$w ( “#transit3” ). hide ();
}
});

@jonatandor35 when we use the fieldKey, don’t we need to specify the dataset somehow?