# Display ratings

**URL:** <https://forum.wixstudio.com/t/display-ratings/62178>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [August 24, 2024, 10:11pm UTC](https://forum.wixstudio.com/t/display-ratings/62178 "2024-08-24T22:11:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![francois69997](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/francois69997/32/86156_2.png) [@francois69997](https://forum.wixstudio.com/u/francois69997)\
**Post date:** [August 24, 2024, 10:11pm UTC](https://forum.wixstudio.com/t/display-ratings/62178/1 "2024-08-24T22:11:04Z")

</div>

**Question:**  
Has anyone tried to use the velo code sample to display ratings in a form?  
I have tried several times without success, so I wonder if there is a mistake in the Code sample given by wix. Here is the link:

> **[Capturing and Displaying Ratings](https://dev.wix.com/docs/develop-websites/articles/code-tutorials/wix-editor-elements/capturing-and-displaying-ratings)**
>
> Velo Tutorial: Capturing and Displaying Ratings Visit the Velo by Wix website to onboard and continue learning. This tutorial shows you how you can capture and display ratings of your items from your visitors. You will set up a Ratings Display...

I’m creating a form and would like to collect rating Inputs and show customers the average rating and total customer ratings. I have followed the tutorial to and made the necessary changes in the code, but I only get error message when I run the code. The error message seems to be on the line of code that calculates the average rating?

/François

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [August 25, 2024, 12:34am UTC](https://forum.wixstudio.com/t/display-ratings/62178/2 "2024-08-25T00:34:02Z")

</div>

Hi, francois69997 !!

If you share the code you’ve edited, someone in this community might be able to help you find a solution!

When including code in your message, please select the “\</\>” option and replace the “type or paste code here” text with your code before replying.

like this

```javascript
$w("#dataset1").onReady(() => {
  // get the current item from the dataset
  const currentItem = $w("#dataset1").getCurrentItem();
  // ...
});

```

---

<div class="post-metadata">

**Author:** ![francois69997](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/francois69997/32/86156_2.png) [@francois69997](https://forum.wixstudio.com/u/francois69997)\
**Post date:** [August 25, 2024, 11:05pm UTC](https://forum.wixstudio.com/t/display-ratings/62178/3 "2024-08-25T23:05:22Z")

</div>

Hello There!  
Tks for the reply. Bellow my code with notes (I Hope its understandable. I have done this procedure several times but I the ratingDisplay doesn’t change nor data is submitted to the collection.

// **notes** : I have created in my Collection 3 Number fields

1- Average Rating - field ID = **averageRatings**  
2- Rating Submitted - field ID = **ratingSubmitted**  
3- Total Ratings - field ID = **totalSubmitted**

// I have added a Dataset (id)= **“#dataset1”**  
// a rating display (id)= **“#ratingDisplay1”** - connected to the **“#dataset1”**  
// the rating value connects to = ratingSubmitted  
// Number of ratting connects to = totalSubmitted  
// I have added event handler onChange for the **“#ratingDisplay1”**  
// I have added a rating input (id) **“#ratingInpunt1”)**_not connected to the_ **“#dataset1”**

**The code:**

```javascript
export function ratingsInput1_change(event) {

 $w("#dataset1").onReady(() => {

const currentItem = $w("#dataset1").getCurrentItem();

const average = currentItem.averageRatings;
const count = currentItem.ratingSubmitted;
const total = currentItem.totalSubmitted;
const newRating = $w("#ratingsInput1").value; 

const newAverageLong = (total + newRating) / (count + 1);
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);

$w("#dataset1").setFieldValues({
    averageRatings : newAverageShort,
    totalRatings: total + newRating, 
    rating Submitted: count + 1,
  });

 $w("#dataset1")
    .save()
    .catch((err) => {
      console.log("could not save new rating");
    });
});

}

```

---

<div class="post-metadata">

**Author:** ![Dan\_Suhr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/dan_suhr/32/94074_2.png) [@Dan\_Suhr](https://forum.wixstudio.com/u/Dan_Suhr)\
**Post date:** [August 25, 2024, 11:46pm UTC](https://forum.wixstudio.com/t/display-ratings/62178/4 "2024-08-25T23:46:46Z")

</div>

you have extra spacing here after currentItem.(space)

There may be other issues, but that stood out straight away

> [@francois69997](#):
>
> ```javascript
> const count = currentItem. ratingSubmitted;
> const total = currentItem. totalSubmitted;
> 
> ```

---

<div class="post-metadata">

**Author:** ![francois69997](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/francois69997/32/86156_2.png) [@francois69997](https://forum.wixstudio.com/u/francois69997)\
**Post date:** [August 26, 2024, 6:33am UTC](https://forum.wixstudio.com/t/display-ratings/62178/5 "2024-08-26T06:33:18Z")

</div>

Hi Dan\_Suhr,  
tks for looking at it. The extra space indeed was here but not in my original code. I have rectified the code here just in case. Thanks

/François

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [August 26, 2024, 7:15am UTC](https://forum.wixstudio.com/t/display-ratings/62178/6 "2024-08-26T07:15:54Z")

</div>

Hi, francois69997 !!

Thank you for providing the code. It was very helpful. Based on your excellent hint that there might be an issue with the average value calculation, I reviewed the code and I suspect that there might be a problem with the data type of `newAverageShort`. Since the return value of `toFixed(1)` is a string, it seems that `newAverageShort` ends up being a string. Trying to save this value as the `averageRatings` in the collection field, which is defined as a number type, may be causing an error. Please modify the code as follows.

like this

```javascript
const newAverageShort = Number(newAverageLong.toFixed(1)); // number

```

---

<div class="post-metadata">

**Author:** ![francois69997](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/francois69997/32/86156_2.png) [@francois69997](https://forum.wixstudio.com/u/francois69997)\
**Post date:** [August 26, 2024, 1:56pm UTC](https://forum.wixstudio.com/t/display-ratings/62178/7 "2024-08-26T13:56:22Z")

</div>

Hi,  
Tks for the input. I try to change that code line, alas it didn’t work.

I have replaced:

```javascript
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);
// with 
const newAverageShort = Number(newAverageLong).toFixed(1);

```

These are the errors I get:

// UserError: An error occurred in one of undefined callbacks  
Caused by TypeError: Cannot read properties of null (reading ‘averageRatings’)

//Caused by: TypeError: Cannot read properties of null (reading ‘averageRatings’)

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [August 26, 2024, 4:51pm UTC](https://forum.wixstudio.com/t/display-ratings/62178/8 "2024-08-26T16:51:18Z")

</div>

Hi, please double-check your code carefully!!

My code

```javascript
const newAverageShort = Number(newAverageLong.toFixed(1)); // number

```

Your code

```javascript
const newAverageShort = Number(newAverageLong).toFixed(1);

```
