Getting SDK Error when using Ratings element

I am getting a warning followed by an error when testing the ratings element. The error occurs even when I add a starter number in the collection. Can anyone please help?

Error:
“Wix Code SDK Warning: The rating parameter that is passed to the rating method cannot be set to null or undefined. It must be an integer.”

Used code:
// For full API documentation, including code examples, visit http://wix.to/94BuAAs

$w.onReady(function () {
//TODO: write your page related code here…

});

export function dropdown1_change(event, $w) {
// get the current item from the dataset
const currentItem = $w(“#dataset1”).getCurrentItem();
// get the current average rating and number of ratings
const average = currentItem.averageRating;
const count = currentItem.numRatings;
// get the new rating from the dropdown and make it a number
const newRating = Number($w(‘#dropdown1’).value);
// calculate the new average rating based on the current average and count
const newAverage = ((average * count) + newRating) / (count + 1);
// save new average rating and total ratings to the collection
$w(‘#dataset1’).setFieldValues( {
‘impressAvg’: newAverage,
‘impress’: count + 1
} );
$w(‘#dataset1’).save()
.catch( (err) => {
console.log(‘could not save new rating’);
} );
// disable the dropdown
$w(‘#dropdown1’).disable();

//Add your code for this event here:  

}

I figured out the error and fixed it.

Hi,
Please share the solution in case someone else runs into the same problem :slight_smile:

Solution:
Error was caused by not changing:

const average = currentItem.averageRating;
const count = currentItem.numRatings;

to reflect the actual fields in my database collection:

const average = currentItem.impressAvg;
const count = currentItem.impress;

Thanks

Hi, im experiencing the same error.

I put the rating input element in a repeater. The ideia is to user choose someone on a dropdown element, click a button to run a query and the repeater shows the result with some elements (one of them is a rating display with actual rating value; and I included a input to user avaliate).

import wixData from ‘wix-data’ ;

export function dd1_change(event) {
$w( “#button1” ).show()
.then( () => {
console.log( “Done with show” );
})
}

export function button1_click(event) {

wixData.query( “subcategorias” )
.eq( “nomeDoProfessor” , $w( “#dd1” ).value)
.ascending( “nomeDoProfessor” )
.find() // Run the query
.then( (results) => {

let fadeOptions = {
“duration” : 1000 ,
“delay” : 500
};
($w( “#repeater2” ).show( “fade” , fadeOptions)
)
console.log(results.items);

});
}

export function notasInput_change(event) {

//$w(“#dataset2”).onReady(() => {

const currentItem = $w( ‘#dataset2’ ).getCurrentItem();

const notaatual = currentItem.avaliacoes;
const contagem = currentItem.quantidadeAvaliacoes;
const total = currentItem.somaAvaliacoes;

const novaNOTA = $w( ‘#notasInput’ ).value;

const novaMediaLonga = (total + novaNOTA) / (contagem + 1 );

const novaMediaArredondada = Number.parseFloat (novaMediaLonga).toFixed( 1 );

$w( “#dataset2” ).setFieldValues({
‘avaliacoes’ : novaMediaArredondada,
‘quantidadeAvaliacoes’ : (contagem + 1 ),
‘somaAvaliacoes’ : total + novaNOTA
});

$w( ‘#dataset2’ ).save()
. catch ((err) => {
console.log( ‘erro ao salvar’ )
});
//});

//})
}


ps: (FIELDS Keys of a database named subcategorias)
somaAvaliacoes = sum (total ratings)
quantidadeAvaliacoes = quantity of ratings
avaliacoes = rating input

ps2: Look the screenshot

LinK:
https://fabioprofessor.wixsite.com/my-site-2/cópia-avaliacoes

P L E A S E H E L P ! ! !