Check for data in dataset

I am trying to create an offers page which is linked to a dataset to automatically display offers>

However, when there are no offers its just blank.

Is there some code to check for data, and if there is none to show a text box?

Thanks.

So you mean that your dataset contains no records and if it does you want to show text box?

If your dataset has the id of dataset1 try the below

let allRecords = $w("#dataset1").getItems(0,1000);
let countRecords = allRecords.items.length;
if (countRecords === 0) {
  $w("#box1").show();
} else {
  $w("#box1").hide();
}

Thank you for the reply,

It seems to be returning this error

'TypeError: Cannot read property ‘length’ of undefined ’

with this code

$w.onReady( function () {
//TODO: write your page related code here…
let allRecords = $w(" #dataset1 “).getItems(0,1000);
let countRecords = allRecords.items.length;
if (countRecords === 0) { $w(” #group1 “).show();
} else { $w(” #group1 ").hide();
}
});

let allRecords = $w("#dataset1").getItems(0,1000); 
if (allRecords && allRecords.items) {
  let countRecords = allRecords.items.length; 
} else {
  let countRecords = 0;
}
if (countRecords === 0) { $w("#box1").show(); } else { $w("#box1").hide(); } 

Hi Connor,

The are two problems at your code:
First , a dataset needs to load its data before you call its getItems() function. Usually a dataset finishes loading a short time after the page it is on finishes loading. So if you call getItems() inside the page’s onReady() event handler, the dataset might not be ready yet.
To call getItems() as soon as possible after a page loads, use the dataset’s onReady() function inside the page’s onReady() event handler to ensure that both the page and the dataset have finished loading.

second , the getItems() function returns a promise that is resolved to a object when the items have been retrieved. As a result, your code continue to run before the promise is resolved.

Follow the code below:

$w.onReady(function () {
  $w('#dataset2').onReady(() => {
        $w("#dataset2").getItems(0, 1000)
            .then((allRecords) => {
            let countRecords = allRecords.items.length;
             if (countRecords === 0) {
                    $w("#button7").show();
                } else {
                    $w("#button7").hide();
                }
            });
    });
 })

Have a nice day and best of luck!
Sapir,

Thank you Sapir!

That works perfectly and is exactly what i needed thank you.