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