How to check if document field is empty?

Hello wonderful people,

On a dynamic page I have a button ‘downButton’ which is linked to the ‘document’ field in same dataset. I need the button lable to change depending on if the document is empty or has some document uploaded.

Following is the code am using which isn’t working:

$w.onReady(function () {

if ($w("#downButton").link !== null) {	 
	$w('#downButton').label = "Download Judgment"; 
} 
else { 
	$w('#downButton').label = "Judgment to be uploaded soon"; 

}
});

Console shows “Wix code SDK Warning: The link parameter that is passed to the link method cannot be set to null or undefined”, twice on each run.

Please guide how to set it right.

Thanking in anticipation.

Anupam Dubey

Hey Anupam,

Thanks for using WixCode and for your question. You addressed your question to the “wonderful people”, but I’ll answer instead :wink:.

First off, don’t worry about the warning - it is just that, a warning. Since you’ve linked the button to the document field, you are being warned that the field is empty. Of course, you’ve stated that it might be empty, which is what you’re trying to find out. Now, how to find out if the field is empty…

You need to check the document field via the dataset. The following snippet of code shows how to retrieve the field, and how to check if a document exists or not.

$w.onReady(function () { 
	var doc = $w("#dynamicDataset").getCurrentItem().doc;
	if(doc === null) {
		console.log("no document found");
	}
	else {
		console.log("document: " + doc);
	}
});

Note: I use console.log() all the time so that I know what’s going on - console.log() is your friend.

I hope this helps.

All the best,

Yisrael

Hi Yisrael,

You too are wonderful dear.

Thanks for the response! I’ll try this out and let you know.

You too are wonderful dear.

Anupam Dubey

Anupam - did you work this out?
I am struggling with the same issue and could do with a more in depth explanation of the solution

Hi Yisrael,

With this code now it isn’t running when there’s no document, everytime console only logs

document: undefined

Now how to proceed?

No Mark, am still searching for solution.

Does it work when there is a document? ie. deliver the label “Download Judgment”?

@Mark, my problem is solved!

Please check this thread, it’ll be helpfull.

@Yisreal, can you please help me with this?

Thanking in anticipation.

Anupam Dubey

Great work