Display ratings

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:

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

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

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

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:

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");
    });
});

}

you have extra spacing here after currentItem.(space)

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

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

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

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

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

I have replaced:

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’)

Hi, please double-check your code carefully!!

My code

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

Your code

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