# Read field from collection

**URL:** <https://forum.wixstudio.com/t/read-field-from-collection/37385>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [August 7, 2022, 1:14pm UTC](https://forum.wixstudio.com/t/read-field-from-collection/37385 "2022-08-07T13:14:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prestonkelpiek9](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/prestonkelpiek9/32/32720_2.png) [@prestonkelpiek9](https://forum.wixstudio.com/u/prestonkelpiek9)\
**Post date:** [August 7, 2022, 1:14pm UTC](https://forum.wixstudio.com/t/read-field-from-collection/37385/1 "2022-08-07T13:14:44Z")

</div>

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”

```javascript

$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

 ![](https://us1.discourse-cdn.com/wix/original/3X/b/7/b7b17dcc5dc3362d4b39f1b0b816c7d26a36b111.jpeg)  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [August 7, 2022, 2:44pm UTC](https://forum.wixstudio.com/t/read-field-from-collection/37385/2 "2022-08-07T14:44:03Z")

</div>

Depends on, if you use a DATASET or not!

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

```javascript

$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.

---

<div class="post-metadata">

**Author:** ![prestonkelpiek9](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/prestonkelpiek9/32/32720_2.png) [@prestonkelpiek9](https://forum.wixstudio.com/u/prestonkelpiek9)\
**Post date:** [August 7, 2022, 2:59pm UTC](https://forum.wixstudio.com/t/read-field-from-collection/37385/3 "2022-08-07T14:59:26Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [August 7, 2022, 3:51pm UTC](https://forum.wixstudio.com/t/read-field-from-collection/37385/4 "2022-08-07T15:51:07Z")

</div>

## 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](https://www.wix.com/velo/reference/wix-data/wixdatafilter/isempty)

My site is → OUT OF SERVICE at moment.
