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 http://wix.to/94BuAAs
$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/
Make sure your collection's permissions allow for user input.
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.