Review statistics updated fail

Hey!

I used a Yoav tutorial to create a review system that its not working perfectly.

I have 2 problems:

1- The Review stats database is not being uploaded (IDK why).
2- The users can submit just 1 review, after that all the reviews are upload but not updated to the different databases. The lightbox is also not closing at the end.

Tutorial: https://www.wix.com/code/home/forum/wix-tips-and-updates/example-store-reviews-rating

What should I do?

Thank you very much!

PS: My code on the light box (the main problem for updating statistics):

import wixWindow from 'wix-window';
import wixData from 'wix-data';

let companyId;
let rated = false;
let reviewRating;

$w.onReady(function () {
    companyId = wixWindow.lightbox.getContext().companyId;
 
    initStars();

    $w('#SubmitReviews').onBeforeSave(() => {
        console.log('beforeSave');
 if (!rated) {
            $w('#rateError').show();
 return Promise.reject();
        }
        $w('#SubmitReviews').setFieldValues({
            companyId,
            rating: reviewRating || 0,
            recommends: $w('#radioGroup1').value
        });
    });
 
    $w('#SubmitReviews').onAfterSave(async () => {
 await updateStatistics($w('#radioGroup1').value);
        wixWindow.lightbox.close();
    });
});

const blueStars = ["starBlue1", "starBlue2", "starBlue3", "starBlue4", "starBlue5"];
const grayStars = ["starGray1", "starGray2", "starGray3", "starGray4", "starGray5"];

function initStars() {
 for (let i = 0; i < grayStars.length; i++) {
 const numberOfStar = i+1;
 const grayStar = $w(`#starGray${numberOfStar}`)
 const blueStar = $w(`#starBlue${numberOfStar}`)

        grayStar.onMouseIn((event) => {
            fillStars(numberOfStar);
        });
 
        blueStar.onMouseIn((event) => {
            fillStars(numberOfStar);
            rated = true;
            reviewRating = numberOfStar;
        });
    }
}

function fillStars(numberOfStars) {
    $w('#rateError').hide();
 
 for (var i = 0; i < blueStars.length; i++) {
 const numberOfStar = i+1;
 let star = $w('#'+blueStars[i]);
 if (numberOfStar <= numberOfStars) {
            star.show();
        } else {
            star.hide();
        }
    }
}

async function updateStatistics(isRecommended) {
 let stats = await wixData.get('ReviewStats', companyId);
 if (stats) {
        stats.rating += reviewRating;
        stats.count += 1;
        stats.recommended += isRecommended?1:0
 return wixData.update('ReviewStats', stats)
    }
 
    stats = {
        _id: companyId,
        rating: reviewRating,
        count: 1,
        recommended: isRecommended?1:0
    };
 return wixData.insert('ReviewStats', stats)
}

I need help :frowning:

Hi,
You can use the example of it here . If you copy the code from the example, please make sure that the IDs, the names of the collections and the collections permissions are the same as the example.

Good luck!
Tal.

Hi Tal, the example there isn’t working for me, i’ve edited every field and dataset to match mine but no luck! The average rating isn’t upadting in my review-stats dataset. Nothing. So i must be missing something. The reviews are updated perfectly.

I used the code from here as an example:

I’ve been trying for the last week, but i can’t see any errors.