Ratings Calculation Not Working!

Let me preface this post by stating that I copied the code from this example product page .

On my live site, the #reviews show up, the lightbox works as it should, and I’m able to post and see reviews, but the #generalRatings element (ratings display element; displays 5 stars with the average rating and the number of reviews) doesn’t do the following:

  • Display the average #rating

  • Display the number of reviews

The #recoPercent element (text element; displays the percentage of reviewers that recommend the product) also does not update.

The example site has 56 #reviews on it and it seems to be working fine. I tested with 5 reviews with different ratings and nothing populates. Per the example, it doesn’t look like either of these elements connect to anything specific inside of the Reviews dataset.

Here’s the calculation code that is currently on my site (same as the example product page code):

// Load the current product's statistics.
async function loadStatistics() {

// Get the statistics data based on the current product's ID.
const stats = await wixData.get('review-stats', product._id);

// If statistics data for the product was found:
if (stats) {

// Compute the product's average rating by dividing the total points by the number of ratings.
let avgRating = (Math.round(stats.rating * 10 / stats.count) / 10);

// Compute the percentage of reviewers that recommend the product.
let percentRecommended = Math.round(stats.recommended / stats.count * 100);

// Get the ratings element.
let ratings = $w('#generalRatings');

// Set the ratings element's average rating to the value calculated above.
ratings.rating = avgRating;

// Set the ratings element's number of ratings to the count value from the statistics data.
ratings.numRatings = stats.count;

// Set the text for the recommended percentage element.
$w('#recoPercent').text = `${percentRecommended} % would recommend`;

// Show the ratings element.
$w('#generalRatings').show();

// If there is no statistics data for the product:
} else {

// Set the text for the recommended percentage element to reflect the fact that there are no reviews.
$w('#recoPercent').text = '';
}

Is there something here that I’m supposed to customize that I may have skipped? Would really appreciate the help. Thank you!

Hi.

I have checked your code and everything appears to be correct. However, it’s not possible to understand the issue basing on what you have provided.

Go through this article to try to fix the issue and if you can’t figure it out send a link to your site editor here, and we shall take a closer look.

Good luck!

Thank you so much for your response. Here’s the link to my site editor .

Super quick update: I figured it out. I needed to change the permissions on the review-stats database. It seems like it’s working now.

Hi, what permissions did you change it to, to get it working?
I seem to be having the same issue

Hi! I changed it to custom use and set everything except “Who can delete content from this collection?” to anyone. The last one I set to Admin. I attached a pic to show you what mine looks like.

I hope this helps!

thank you, just tried that but hasn’t changed anything unfortunately :disappointed_relieved:

please helppp!!! i’m getting mad! i put all the correct code 10 differents time still not working and i got that error code can you help me out so i can use properly my website please?

wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==:1 Failed to load resource: the server responded with a status of 403 ()
b2f6ded6-18de-45bc-b401-cfbaf827e269:65 [Deprecation]‘webkitRequestAnimationFrame’ is vendor-specific. Please use the standard ‘requestAnimationFrame’ instead.
nrWrapper @ b2f6ded6-18de-45bc-b401-cfbaf827e269:65
workerLogger.js:103 Loading the code for the Product Page page. To debug this code, open cjfw.js in Developer Tools.
5The resource < URL > was preloaded using link preload but not used within a few seconds from the window’s load event. Please make sure it has an appropriate as value and it is preloaded intentionally.
workerLogger.js:103 Loading the code for the Review Box page. To debug this code, open nbr1t.js in Developer Tools.
supplementaryErrorHandlers.js:22 TypeError: Cannot read property ‘productId’ of undefined
at nbr1t.js:15
at wixSelector.ts:287
at tslib.es6.js:102
at Object.next (tslib.es6.js:83)
at tslib.es6.js:76
at new Promise ()
at s (tslib.es6.js:72)
at r (wixSelector.ts:285)
at wixSelector.ts:103
at Array.map ()
r.error @ supplementaryErrorHandlers.js:22
supplementaryErrorHandlers.js:22 save operation failed:
r.error @ supplementaryErrorHandlers.js:22

What “correct code” did you try? In order to get assistance, you will need to provide more information. Share your code in a code block. Are errors being flagged in the code editor?

I used the wix tutorial code for adding reviews and ratings, i followed step by step…in the editor develop mode there is no any no any red anywhere…

here is the product page code
//-------------Imports-------------//

import wixData from ‘wix-data’ ;
import wixWindow from ‘wix-window’ ;

//-------------Global Variables-------------//

// Current product.
let product ;

//-------------Page Setup-------------//

$w . onReady ( async function () {
// Set the global product variable to the currently displayed product.
product = await $w ( ‘#productPage1’ ). getProduct ();
// Load the current product’s reviews using the initReviews() function.
initReviews ();
});

// Loads the current product’s reviews.
async function initReviews () {
// Filter the “Reviews” dataset to contain only the reviews on the currently displayed product.
await $w ( ‘#Reviews’ ). setFilter ( wixData . filter (). eq ( ‘productId’ , product . _id ));
// Show the reviews after the filter was set and applied on the dataset
showReviews ();
// Load the current product’s statistics using the loadStatistics() function.
loadStatistics ();
}

// Load the current product’s statistics.
async function loadStatistics () {
// Get the statistics data based on the current product’s ID.
const stats = await wixData . get ( ‘review-stats’ , product . _id );
// If statistics data for the product was found:
if ( stats ) {
// Compute the product’s average rating by dividing the total points by the number of ratings.
let avgRating = ( Math . round ( stats . rating * 10 / stats . count ) / 10 );
// Compute the percentage of reviewers that recommend the product.
let percentRecommended = Math . round ( stats . recommended / stats . count * 100 );
// Get the ratings element.
let ratings = $w ( ‘#generalRatings’ );
// Set the ratings element’s average rating to the value calculated above.
ratings . rating = avgRating ;
// Set the ratings element’s number of ratings to the count value from the statistics data.
ratings . numRatings = stats . count ;
// Set the text for the recommended percentage element.
$w ( ‘#recoPercent’ ). text = ${ percentRecommended } % would recommend ;
// Show the ratings element.
$w ( ‘#generalRatings’ ). show ();
// If there is no statistics data for the product:
} else {
// Set the text for the recommended percentage element to reflect the fact that there are no reviews.
$w ( ‘#recoPercent’ ). text = ‘There are no reviews yet’ ;
}
// Show the recommended percentage element only after it is populated to avoid flickering.
$w ( ‘#recoPercent’ ). show ();
}

//-------------Repeater Setup -------------//

// Set up each item in the reivews repeater as it is loaded.
export function reviewsRepeater_itemReady ( $w , itemData , index ) {
// If the reviewer recommends the item:
if ( itemData . recommends ) {
// Set the "recommend text.
$w ( ‘#recommendation’ ). text = ‘I recommend this product.’ ;
// If the reviewer does not recommend the item:
} else {
// Set the “don’t recomend” text.
$w ( ‘#recommendation’ ). text = “I don’t recommend this product.” ;
}

// If a photo was uploaded for the review:
if ( itemData . photo ) {
// Set the image URL from the item data.
$w ( ‘#reviewImage’ ). src = itemData . photo ;
// Expand the image.
$w ( ‘#reviewImage’ ). expand ();
}

// Set the ratings element’s rating value.
$w ( ‘#oneRating’ ). rating = itemData . rating ;

// Get the date that the review was entered.
let date = itemData . _createdDate ;
// Format the date according to the date format settings on the user’s computer.
$w ( ‘#submissionTime’ ). text = date . toLocaleString ();
}

//-------------Data Setup -------------//

// Perform some setup when the dataset filter was completed.
export function showReviews () {
// If at least one review has been submitted:
if ( $w ( ‘#Reviews’ ). getTotalCount () > 0 ) {
// Expand the strip that displays the reviews.
$w ( ‘#reviewsStrip’ ). expand ();
// If there are no reviews:
} else {
// Collapse the strip that displays the reviews.
$w ( ‘#reviewsStrip’ ). collapse (); //otherwise, hide it
}
}

//-------------Event Handlers -------------//

// Set the action that occurs when a user clicks the “Write a Review” button.
export async function addReview_click ( event , $w ) {
// Create an object containing the current product’s ID to be sent to the review writing lightbox.
const dataForLightbox = {
productId : product . _id
};
// Open the “Review Box” lightbox, send it the object created above, and wait for it to close.
let result = await wixWindow . openLightbox ( ‘Review Box’ , dataForLightbox );
// After the review lightbox is closed, refresh the reviews dataset so the new review appears on the page.
$w ( ‘#Reviews’ ). refresh ();
// Reload the current products statistics to reflect the new rating.
loadStatistics ();
// Show a thank you message.
$w ( ‘#thankYouMessage’ ). show ();
}

// Set the action that occurs when a user clicks the “Load More” text.
export function resultsPages_click ( event , $w ) {
// Load additional reviews into the reviews repeater.
$w ( ‘#Reviews’ ). loadMore ();
}

/**

/**

/**

/**

and then here my review box code
//-------------Imports-------------//

import wixWindow from ‘wix-window’ ;
import wixData from ‘wix-data’ ;

//-------------Global Variables-------------//

// Current product’s ID.
let productId ;

//-------------Lightbox Setup-------------//

$w . onReady ( function () {
// Get the data passed by the page that opened the lightbox.
productId = wixWindow . lightbox . getContext (). productId ;

// Set the action that occurs before the review is saved.
$w ( ‘#SubmitReviews’ ). onBeforeSave (() => {
// If no rating was set:
if ( $w ( ‘#radioRating’ ). value == 1 ) {
// Display an error message.
$w ( ‘#rateError’ ). show ();
// Force the save to fail.
return Promise . reject ();
}

// If a rating was set, set the element values into the fields of the dataset item.
// These values will be saved in the collection.
$w ( ‘#SubmitReviews’ ). setFieldValues ({
productId ,
rating : $w ( ‘#radioRating’ ). value ,
recommends : $w ( ‘#radioGroup1’ ). value
});
});

// Set the action that occurs after the review is saved.
$w ( ‘#SubmitReviews’ ). onAfterSave ( async () => {
// Update the product’s statistics using the updateStatistics() function.
await updateStatistics ( $w ( ‘#radioGroup1’ ). value );
// When the statistics have been updated, close the lightbox to return the user to the product page.
wixWindow . lightbox . close ();
});
});

// Update (or create) the product statistics.
async function updateStatistics ( isRecommended ) {
// Get the review statistics for the current product from the “review-stats” collection.
let stats = await wixData . get ( ‘review-stats’ , productId );

// If statistics data already exist for this product:
if ( stats ) {
// Add the new rating to the total rating points.
stats . rating += parseInt ( $w ( ‘#radioRating’ ). value , 10 );
// Increase the ratings count by 1.
stats . count += 1 ;
// Increase the recommendations by one if the user recommends the product.
stats . recommended += ( isRecommended === “true” ) ? 1 : 0 ;
// Update the new product statistics in the “review-stats” collection.
return wixData . update ( ‘review-stats’ , stats )
}
//If no statistics data exists for this product, create a new statistics item.
stats = {
// Set the statistics item’s ID to the current product’s ID.
_id : productId ,
// Set the statistics item’s rating to the rating entered by the user.
rating : parseInt ( $w ( ‘#radioRating’ ). value , 10 ),
// Set the statistics item’s ratings count to 1 because this is the first rating.
count : 1 ,
// Set the statistics item’s recommended property to 1 if the user recommends the product.
recommended : ( isRecommended === “true” ) ? 1 : 0
};
// Insert the new product statistics item into the “review-stats” collection.
return wixData . insert ( ‘review-stats’ , stats )
}

//-------------Event Handlers-------------//

// Set the action that occurs when a rating is chosen.
export function radioRating_change ( event , $w ) {
// Hide the error message.
$w ( ‘#rateError’ ). hide ();
}

/**

in the preview mode i got theis message
Loading the code for the Review Box page. To debug this code, open nbr1t.js in Developer Tools.

Loading the code for the Product Page page. To debug this code, open cjfw.js in Developer Tools.

TypeError: Cannot read property ‘productId’ of undefined

What tutorial did you use? What’s the link?

I see in your code, that you have two functio ns (addReview_click_1 and addReview_click_1) f or the same button, both of which are empty. Which one of those functions is connected to the button? That function will of course require some code.

the addReview button is on the product page and the fonction is to open the review box to post the review, but not working so i link it with the light box…
the link i used is
Velo Tutorial: Adding Ratings and Reviews to a Wix Stores Site | Help Center | Wix.com

@garlattigael If the function isn’t working, then you will need to determine why. The code for the button function is needed in order to send the productId to the Lightbox, and is the reason that you are getting this error (that you posted):

supplementaryErrorHandlers.js:22 TypeError: Cannot read property ‘productId’ of undefined

You will need to go over the tutorial again and make sure that you have accurately added all of the correct and necessary code to ensure the proper operation of your site.

Take a look at Product Reviews for a working example of the tutorial.

i just changed it, still not working…i been almost 10 times over the tutorial…i’m done, please any ingenieur or whoever it is please help me to fix this…

Please post the URL of your site.

Also, try running the Product Reviews example to see how it works.

@yisrael-wix the url is www. rangzen yoga . Com

You’ll have a 1st light box but click on the top right corner and it should disappear (it does for me)
I will try the exemple tonight as i’m not free now.

But you can write here and i will read you😉.

Thank you for your kind help.:v::muscle: