I have a display rating to get the count and average rating from my datasets. but not working, undefined.

I have seen this article https://support.wix.com/en/article/corvid-tutorial-capturing-and-displaying-ratings . But it doesn’t really serves my requirement. What i have now is a lightbox that capture user’s rating input with their basic info, rating from 1 to 5, and rating description. Once submitted in the lightbox, it will do a write back to one of my dataset and then exit the lightbox.

Once user exit the lightbox and redirect to a page, i want to display the display rating that is the total and average of all reviews submitted by everyone. 3 parts to a typical display rating - on the left, it will capture the average rating in number, the middle part will be the number of stars as per the average, the right part will be the count of total ratings submitted.

The code i have now works for the right part to get the count. but the first two has ‘undefined’. I am very new to wix coding and hopefully get some help here.

import wixBookings from ‘wix-bookings’;
import wixData from ‘wix-data’;

wixData.query(“ReviewDataset”)
.count() //getting count of all reviews
.then( (num) => {
console.log(num);
$w(“#ratingsDisplay1”).numRatings = Number.parseInt(num) ; //putting count into display rating
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );

wixData.aggregate(" ReviewDataset “)
.avg(“customerRating”) //get average of all fields from ReviewDataset.customerRating
.run()
.then( (results) => {
$w(”#ratingsDisplay1").rating = Number.parseNum(results) ; //put average value to display rating
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );

I think the red font part above for the avg is not working. the rating part of the display rating requires a number field and i think the avg results is not in number. How can i cast it to number?

1 Like

See the Ratings by User example.

Hi Yisrael, I have seen that too. But it’s quite different as i do not have the user input rating side by side with the display rating. I will need to get the statistics from the datasets for the display rating.

I googled alot and seems like avg() function from wixData.aggregate can work. However, it returns an array with multiple variables. I am finding ways to only get the real avg number out and assign it to the display rating. Any idea how?

i have a form @yisrael-wix and i want to display star ratings from that form with a average rating can u please guide me

Did u find the solutions for this i am trying to do the same / also when u do the count u need to specify the column from the dataset (which u want to count)