I’m having a difficult time computing calculated values off of my ratings database. I’ve attempted to follow the guide below exactly:
I have a feeling that I am not referencing the data tables correctly and/or they may not be set up correctly.
My Reviews table has my product linked to it, and I also have the ID field.
My review-stats table has the ID field, as well as the headers mentioned in the support site.
Any help would be much appreciated.
My code is below:
//-------------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( ‘_id’ , 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( ‘#ratingsDisplay1’ );
// Set the ratings element’s average rating to the value calculated above.