Star rating triples up with each click

I can´t figure out why my rating triples up each time you click on a star, from 52 to 55 and from 55 to 58 etc? Here is my code and I have made the fields heterosexRating, heterosexNumber and heterosexTotal which I have connected to the dataset. Someone wrote that you shall not connect the input element to the dataset (only the display element) but when I don´t do that nothing happends when you click on a star. Very frustrating. I guess that the tripled acounting depens on that I have connected the input element or what is your guess? :wink:

export function ratingsInput1_change(event) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
$w(“#dataset2”).onReady(() => {
// get the current item from the dataset
const currentItem = $w(“#dataset2”).getCurrentItem();

// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.heterosexRating;
const count = currentItem.heterosexNumber;
const total = currentItem.heterosexTotal;

// 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(‘#dataset2’).setFieldValues({
‘heterosexRating’: newAverageShort,
‘heterosexTotal’: total + newRating,
‘heterosexNumber’: (count + 1)
});

// save the dataset fields to the collection 

$w(‘#dataset2’).save()
.catch((err) => {
console.log(‘could not save new rating’);
});
});
// Add your code for this event here:
}

For starters, your $w(“#dataset2”).onReady(() function should be inside your page load $w.onReady( function() so that it is not executed every time the ratingsInput1 button is clicked. More console.log statements may help also.

Okay, so I shall write two different .onReady or do I missunderstand you? I also wonder why you shall not connect the input element to the database? Like I wrote in my description; when I don´t connect it I get no rating at all.

Your code should start with $w.onReady( function() { to ensure the page loads before accessing any page elements. Then your $w(“#dataset2”).onReady(() => { should run to ensure your dataset is ready to access your collection elements. No need for more page or dataset onReady statements.

$w.onReady( function() {
$w(“#dataset2”).onReady(() => {
.
.
});
});
For further explanation, see https://www.wix.com/velo/forum/tips-tutorials-examples/w-onready-a-guide-for-the-perplexed
Hope this helps.

Thanks for your clear description and for the link!

1 Like

I still don´t succede with making it work. If I don´t connect the input element to the database nothing happends and when I do that the display element shows no change or triples up. Very strange so please help me.

I wonder over two strange things Colin:

  1. If you put export function ratingsInput1_change(event) { below $w.onReady( function() { you get the text that exports shall be on the top level, with other words over $w.onReady( function() { so shall I put the ratingsinput over $w.onReady( function() { or not?

  2. Wix instructions say that you shall not connect the input element to the database but on another page you get this instructions (very confusing):

Step 2 | Connecting your ratings input element
Connect your ratings input element to collect your users ratings in a database collection.
To connect your input element to a dataset:

  1. Click the ratings input element.

  2. Click the Connect to Data icon.

  3. Click Connect a dataset to choose a dataset from the drop-down list.

  4. Click Rating value connects to :

  • Choose an existing field in your collection.

  • Click Add a new collection field to create a new field in your collection.

  • Enter a field name.

  • Select a field type.

Event handlers that are created by Property and Events panel are separate functions and not included in the main code headed by onReady statements, sorry if I misled. You might have to delete and re-create the handler if it has been moved. Use console.log statements to check if the handler is working, and if your values are calculated and converted to displayable formats.
Input elements (boxes) can be connected to a database, or not, whatever is needed.
If all else fails, if I can see what your collection looks like, maybe I can re-create your code here…

Would you mind to spend 15 minutes directly in my page editor to check things out and code it correctly because I have never used console.log´s and have not found how to use it. For an expert like you I guess it would be a quick affair. Please send me an email to kricke73@gmail.com and I will give you access.

Colin, are you Peter Glick? (security question).

Yes

Good to know :slightly_smiling_face:

@kricke73 For future reference, such solicitation is against our community guidelines . If you need help, you can hire a Velo Certified Developer in the Wix Marketplace .

@marlowe-shaeffer Okay but their services are really expensive, often from 80 dollar and upwards.