When the page loads, I am trying to pull data from one Database Collection (dataset1), get the total entry count from the database (totalData), get the sum all number entries of a given property (sumRatings), then update another Database Collection (dataset3) with these two values (average, number).
I have produced the following script so far, but I need some guidance. Any help would be appreciated :)
Did I run the sum calculation correctly?
Do I need to call the current item for #dataset3? (there's only ever 1 item)
Do I need to add an additional onReady function for #dataset1?
// Begin Code
import wixData from 'wix-data';
$w.onReady( function() {
const totalData = $w('#dataset1').getTotalCount();
wixData.aggregate('dataset1')
.sum('ratings')
.run()
.then( (results) => {
let sumRatings = results.items;
const newAverageLong = (sumRatings) / (totalData);
// Round the average rating to 1 decimal point
const newAverageShort =
Number.parseFloat(newAverageLong).toFixed(1);
$w("#dataset3").onReady( () => {
$w('#dataset3').setFieldValues({
'average': newAverageShort,
'number': totalData
});
$w("#dataset3").save();
});
});
});
// End Code
Thank you. I did review that tutorial before. The issue is that I'm trying to setup a review AND ratings feature. I already got the reviews to work with individual ratings submitted through a form. That data is populated to dataset1. However, for aggregate ratings to work like in the tutorial, I need different data columns, so I created a separate database (dataset3). I just want to get the average and total number of the ratings submitted to dataset1 through the form and have that display visually using the ratings element.
This seems like something that should be simple to do. I'm surprised there isn't a built in away to do this as it's a standard feature for eCommerce platforms.
Check a look at the Wix Ratings tutorial as that might be a good starting point for you to work off of.
https://support.wix.com/en/article/corvid-tutorial-capturing-and-displaying-ratings