Error when using Rating

I’m getting following error when trying to use Rating on my pages. However when I refresh the page and try it again it works without any issues.

Has any see this before?

Wix code SDK error: The rating parameter that is passed to the rating method cannot be set to the value NaN. It must be of type number.
Wix code SDK error: The numRatings parameter that is passed to the numRatings method cannot be set to the value NaN. It must be of type integer.

Code:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com

$w.onReady( function () {
//TODO: write your page related code here…

});

export function ratingsInput1_change(event) {
$w(“#dynamicDataset”).onReady(() => {
// get the current item from the dataset
const currentItem = $w(“#dynamicDataset”).getCurrentItem();

// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.avg;
const count = currentItem.numRatings;
const total = currentItem.totalRatings;

// get the new rating from the ratings input
const newRating = $w(‘#ratingsInput1’).value;

// calculate the new average rating based on the current
//average and count
const newAverageLong = (total + newRating) / (count +1);
// Round the average rating to 1 decimal point
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);

// set the dataset fields to the new average, total
// ratings, and number of ratings
$w(‘#dynamicDataset’).setFieldValues({
‘avg’: newAverageShort,
‘totalRatings’: total + newRating,
‘numRatings’: (count + 1)
});

// save the dataset fields to the collection
$w(‘#dynamicDataset’).save()
. catch ((err) => {
console.log(‘could not save new rating’);
});
});
}

Looks like your form is returning an empty string.

You are following this tutorial here I assume.
https://support.wix.com/en/article/corvid-tutorial-capturing-and-displaying-ratings

Also, make sure that you change this lines here to reflect the actual fields in your own dataset:
const average = currentItem.avg;
const count = currentItem.numRatings;
const total = currentItem.totalRatings;

Change them to
const average = currentItem.yourfield;
const count = currentItem.yourfield;
const total = currentItem.yourfield;

Also make sure that you followed the tutorial and made the correct field type choice in your dataset/

  1. Make sure your collection’s permissions allow for user input.

  2. Add 3 new Number fields, for the average rating, number of ratings submitted, and sum of all the ratings submitted. You can leave these fields blank or input a starting values. Remember that the average rating must be between 1 and 5.

givemeawhisky Thank you for the feedback, that’s one of the first things I checked. I just looked again field key is listed correct in the code. It should be the Field Key not the name, correct?

@rozdzynski

No not field keys, in this code it should be field names.

@givemeawhisky I updated to the Field Name, here is what I’m seeing in the log

Wix code SDK Warning: The rating parameter that is passed to the rating method cannot be set to null or undefined.
Wix code SDK Warning: The numRatings parameter that is passed to the numRatings method cannot be set to null or undefined.

@givemeawhisky I just noticed I only get this error where there is no entries for the specific rating. If there is at least one rating completed there are no error when pressing the rating.

@rozdzynski

Yes and hence the reason why you were getting the Nan error and then the null or undefined. If you having in your dataset then the ratings can’t be compared against previous ratings etc.

@givemeawhisky so the only solution is to start with one rating already in the database?