Read field from collection

I’m trying to have a a text element on my page read a number value from a dataset and display it

The dataset is named “dynamicDataset”
The fieldkey of the number value within the dataset is named “loyalty”
and the text element is named “loyaltyPoints”


$w.onReady(function () {
 function countItems() {
        let count = $w("#dynamicDataset").loyalty()
        count > 0 ? $w("#loyaltyPoints").text = `You have ${count} points` : $w("#loyaltyPoints").text = `You have 0 Points`;

        return countItems;
    }
});

Obviously I am misusing the technique that should properly call the data from the field value within the collection on line 14 - im wondering if anyone knows how to properly retrieve it so that I can display it


Thanks in advance!

Depends on, if you use a DATASET or not!

How to get DATA from a specific FIELD connected through a DATASET ?


$w.onReady(function(){
  $w("#dynamicDataset").onReady(()=>{
    let currentItem=$w("#dynamicDataset").getCurrentItem();
      console.log(currentItem);
      console.log(currentItem.loyalty);
      console.log(currentItem.title);
      console.log(currentItem._id); //id?
      console.log(currentItem._owner);
       
      $w("#loyaltyPoints").text=`Score:${currentItem.loyalty} points`;
    };
});      

The “count” you were using would always be = 1 on an dynamicDataset.

Dynamic-Dataset, has alsways just → ONE <— ITEM.

Thanks for the response! Your code is working perfectly!!

I hadn’t considered that it would be possible to do without a dataset - but now thinking about it… I guess you don’t need a dataset on the page and you could draw it directly from the collection with code

but I do have a dataset on the page for a bunch of other functions on the page.

I just tried your code and I receive the error: “Score:undefined points”

I assumed this is because the account has 0 points (a blank field) so I went into my content manager and manually gave the account a point and it worked perfectly!

If I could take up a bit more of your time and ask is there a way to have a set message when the field is empty?

Thankyou so much for solving my problem! I’m heading over to your site now to leave you a review :slight_smile:

I hadn’t considered that it would be possible to do without a dataset - but now thinking about it… I guess you don’t need a dataset on the page and you could draw it directly from the collection with code

CORRECT!!!

If I could take up a bit more of your time and ask is there a way to have a set message when the field is empty?

You already answered your own QUESTION!

https://www.wix.com/velo/reference/wix-data/wixdatafilter/isempty

My site is → OUT OF SERVICE at moment.