Capturing and Displaying Ratings

I want to input and then display ratings on my page. so I write this code ( The code is below ), but when I preview this code, the console says: TypeError: $w(…).onReady is not a function.
could you, please tell me what can I do?

Thank you in advance.
Qvelo.

export function ratingsInput1_change(event) {

$w("#blog").onReady(() => {
 // get the current item from the dataset
 const currentItem = $w("#blog").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('#blog').setFieldValues({
 'avg': newAverageShort,
 'totalRatings': total + newRating,
 'numRatings': (count + 1)
 });
 
 // save the dataset fields to the collection
  $w('#blog').save()
 .catch((err) => {
    console.log('could not save new rating');
 });
 });
}

remove this 2 lines

$w("#blog").onReady( ()=> {
...
} );

Also, the onReady function is not used inside a export function
https://www.wix.com/velo/reference/$w/onready

You don’t need to select an element for onReady

Thank you , but now it says that $w(…).getCurrentItem is not a function :frowning:

the code is now like that:

export function ratingsInput1_change(event) {
 // get the current item from the dataset
 const currentItem = $w("#blog").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('#blog').setFieldValues({
 'avg': newAverageShort,
 'totalRatings': total + newRating,
 'numRatings': (count + 1)
 });
 
 // save the dataset fields to the collection
  $w('#blog').save()
 .catch((err) => {
    console.log('could not save new rating');
 });
}


The getCurrentItem() function is for a dataset , and not for the the Blog app, which does not have an API exposed.

I don’t use a blog app which is Wix. I made my blog with the dataset .

So, you have a dataset with the ID #blog?

@yisrael-wix yes, I have

Here it is

You have a collection called blog, but that’s not the same thing as a Dataset . You will need to add a dataset to your page and connect it to the collection. Or, you can use the wix-data API to perform queries and other actions on the collection.

@yisrael-wix I also have a dataset on my page called blogds

Assuming that the #blogds dataset is connected to the blog collection, the line of code should probably be:

const currentItem =$w("#blogds").getCurrentItem();

I recommend reviewing the documentation about connecting data .

I changed it like that:

const currentItem = $w("#blogds").getCurrentItem();

but console says that: TypeError: $w(…).getCurrentItem is not a function :frowning:

@qvelo78 Please post the URL of your site and explain where to see this issue.

@yisrael-wix

this is the blog post page:
I want to add a rating function Below a blog post.