Average rating not working on live site

I have copied the Corvid tutorial and code to work out and show the average ratings after a vote but it won’t work on my live site.
After copying and editing the provided code i tested it in the preview mode and it works fine. However once published, it won’t change the average rating after a vote.

Can anyone help me? Here is my site www.holidays4hounds.co.uk and the rating is at the bottom in the footer.

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

Then make sure that your dataset has the right permissions in your collection itself and on the page where it is used, plus that you have used the dataset’s id name in your code and not just your dataset name.

Also make sure that the three new fields in your dataset are set as number fields and you have also called them correctly so that the field keys match up with what is used in the tutorial, otherwise you will have to edit out the existing field kys and match up your own field keys with the code.

Make sure that you have added the onChange event to the ratings input element in the properties panel.

Finally, just try moving out of the footer and onto your page instead and see if it works fine then, it shouldn’t really make a difference where it is on your page, however there might be a issue with it being in the footer for some silly reason.

This is the code from the tutorial, double check it with yours to see if you’ve missed anything.

import wixData from 'wix-data';

$w.onReady(function () {

export function ratingsInput1_change(event) {
$w("#dataset1").onReady(() => {
// get the current item from the dataset
const currentItem = $w("#dataset1").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('#dataset1').setFieldValues({
'avg': newAverageShort,
'totalRatings': total + newRating,
'numRatings': (count + 1)
});

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

Hi thanks for your reply,
I have double, triple and quadruple checked the code but i may be missing something. The strange thing is that it work in preview mode but not on the published site. Here is the code i have:

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

 // get the current average rating, number of ratings, and
 //total ratings for the current dataset item
 const average = currentItem.averageRate;
 const count = currentItem.numberOfRates;
 const total = currentItem.sumOfRates;

 // get the new rating from the ratings input
 const newRating = Number($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('#dataset1').setFieldValues({
 'averageRate': newAverageShort,
 'sumOfRates': total + newRating,
 'numberOfRates': (count + 1)
        });

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

I have also set the permissions to ‘Anyone’ and ‘Read and Write’

@wilsonaidan101

Okay, firstly I would have just kept the three number fields in the dataset exactly the same field name and field keys, so that they matched the code and you didn’t have to change anything.

With your ratings input settings, make sure that this is not connected to the dataset.

Also, just double check that your field keys are correct, as they are case sensitive a simple upper case character instead of a lower case character can muck you up big time!

As for the code itself, I would change this line here:

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

To what is given to you in the code tutorial:

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

Thanks for your comment but it doesn’t work even after your suggested corrections.

Did you syn c your sandbox database to Live ?

Also, make sure that your collections have the correct permissions .

had the same issue: ratings were updating in preview mode, but not on live site. changed the dataset permissions to custom: Anyone, Anyone, Anyone, Admin. Then it worked on live site.