Check if a document field in dataset is empty

Hi,

I’m using the following code to check if the document field in my dataset is empty…

$w("#datasetISW").onReady(() => {
 let hasDoc = $w('#datasetISW').getCurrentItem().cvURL;
 if (hasDoc) {
            $w("#imgCV").show();
            $w("#txtCVUnavailable").hide();
            console.log("document found");
        } else {
            $w("#txtCVUnavailable").show();
            $w("#imgCV").hide();
            console.log("no document found");
        }
        console.log(hasDoc);
    });

I would like to be able to show/ hide elements based on the outcome but it is always returning as undefined?
Can anyone help me out?
Thanks,
Rachel

Hi, try to print the whole object you get:

$w('#datasetISW').getCurrentItem()

Can you? Do you get undefined for this? In this way you’ll be able to understand what kind of fields are available there. There is a common mistake when a developer tries to get a field value according to its field_name instead of field_key

Hi Rachel:

undefined is a special value in javascript . It is the value you will get if a variable has not been initialised (i.e. to a value or null) or an object is missing the named property. Basically if the object property is not there then it is undefined :-).

The best way to test for this is to use the javascript Object helper function hasOwnProperty() Your code should work if it looks like this:

$w("#datasetISW").onReady(() => {
    let currentItem = $w('#datasetISW').getCurrentItem();
    // Add Eli's proposal
    console.log(currentItem);
    let hasDoc = currentItem.hasOwnProperty('cvURL');
    console.log("cvURL "+(hasDoc?"exists":"does not exist"));
    // Does the data set include a cvURL property for the current item?
    if (hasDoc) {             
            $w("#imgCV").show();
            $w("#txtCVUnavailable").hide();             console.log("document found");
    } else {
            $w("#txtCVUnavailable").show();             $w("#imgCV").hide();
            console.log("no document found");
    }
    console.log(hasDoc);
});

Cheers
Steve

Many thanks for your responses. I know that I am using the correct field value and I have implemented the code you suggested, Steve. Allthough it is not returning as undefined it is still saying ‘no document found’. when there is a value in cvURL . Please see screenshot of the console log below…


Any ideas?

Thanks,
Rachel

Hi, could you provide a link to this site?

Hi, it is markhatterassociates.co.uk

The code is in the page ISW in the function cvAvailability() which is called in btnSearch_click (but currently commented out). To test the function you need to search for –All Regions– and –All Specialisms– in the drop down fields.

Thank you for your assistance :slight_smile:

@rachelskuse You need to spell the property correctly ;-).

your debug output tells the story.

cvUrl : “wix:document://v1...”

The field name is cvUrl not cvURL - :slight_smile:

Steve

@stcroppe Oh dear! What a silly mistake :frowning:
Thank you for pointing it out. All working fine now! :slight_smile:

I know this is cheeky but I am having another issue within the same site. I have posted a couple of times but not had a response. The thread is here…

If there’s any chance you could take a look I’d be eternally grateful! :slight_smile:

I have a similar issue, but am checking to see if the current item in the dataset has an image instead of a document. I modified the code from @stcroppe, but the site is not collapsing the blank images.

import wixData from 'wix-data';
$w("#dataset1").onReady(() => {
let currentItem = $w('#dataset1').getCurrentItem();
console.log(currentItem);
let hasImage = currentItem.hasOwnProperty('image');
console.log("image "+(hasImage?"exists":"does not exist"));
// Does the data set include a cvURL property for the current item?
if (hasImage) {
$w("#serviceImage").show();
console.log("document found");
} else {
$w("#serviceImage").collapse();
console.log("no document found");
}
console.log(hasImage);
});

Thanks! This solved my problem too!
Patricia