Can you please share the code you used to calculate the average? I’m trying desperately to follow the tutorial on Wix about capturing and displaying ratings, but I want to be able to also show things like company name, reviewer name, comments, etc. along with the rating AND average rating + total number of ratings. I tried to careful modify the code, but it’s not working. This is the code I used:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady( () => { ////// I added this extra bit of code to make sure visitors are logged in before reviewing
if (wixUsers.currentUser.loggedIn) {
$w(“#text141”).text = “Share Your Experience”;
$w(“#button2”).show()
}
else {
$w(“#text141”).text = “Please Login First”;
$w(“#button2”).hide();
}
} );
export function button2_click(event) {
$w(“#dataset1”).onReady(() => {
// get the current item from the dataset
const currentItem = $w(“#dataset1”).getCurrentItem();
// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.avg;
const count = currentItem.numRatings;
const total = currentItem.totalRatings;
// get the new rating from the ratings input
const newRating = $w(‘#ratingsInput1’).value;
// calculate the new average rating based on the current
//average and count
const newAverageLong = (total + newRating) / (count +1);
// Round the average rating to 1 decimal point
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);
// set the dataset fields to the new average, total
// ratings, and number of ratings
$w(‘#dataset1’).setFieldValues({
‘venueName’: $w(‘#input2’).value,
‘reviewersName’: $w(“#input1”).value,
‘weddingDate’: $w(“#datePicker1”).value,
‘rating’: $w(“#ratingsInput1”).value,
‘comments’: $w(“#textBox1”).value,
‘avg’: newAverageShort,
‘numRatings’: total + newRating,
‘totalRatings’: (count + 1)
});
////// I’d also like visitors to be able to upload a photo with their review, but haven’t been successful yet
// save the dataset fields to the collection
$w(‘#dataset1’).save()
. catch ((err) => {
console.log(‘ERROR’);
});
});
}
$w.onReady( function () { ///// this bit of code is to disable the review submit button after review is sent
$w(“#button2”).onClick( (event) => {
let $item = $w.at(event.context);
$item(“#button2”).label = “SENT!”;
$item(“#button2”).style.backgroundColor = “#D1D4E0”;
$item(“#button2”).disable()
.then( () => {
console.log(“Button now disabled”);
});
} );
} );