Error while saving updated rating

Hello,

I have setup a collection PEER_GROUP with “Site Content” permission. From my page I have linked dataset1 to this collection. I have a ratings code as described in the example https://support.wix.com/en/article/corvid-tutorial-capturing-and-displaying-ratings.

My code is here:
export function GroupRating_Change(event) {
const avg=SelectedItem.groupVitality
const count=SelectedItem.numberOfRatings
const total=SelectedItem.sumOfRatings
const newRating = $w( ‘#GroupRating’ ).value
console.log( "34 New Rating is " ,newRating)
// 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 );
console.log( "40 New Average in long " ,newAverageLong, " and in short " ,newAverageShort)
// set the dataset fields to the new average, total ratings, and number of ratings
$w( “#dataset1” ).setFieldValues({
‘Group Vitality’ : newAverageShort,
‘Sum of Ratings’ : total + newRating,
‘Number of Ratings’ : (count + 1 )
});

// save the dataset fields to the collection
$w( ‘#dataset1’ ).save()
. catch ((err) => {
console.log( ‘Uh-oh, could not save new rating’ );
})
}

I can see in the console log that I am able to retrieve the existing rating and other values from collection, but while setField Values, the code is failing and I see my error message “Uh-oh, could not save new rating”).

What am I doing wrong?

And, I also tried to replace ‘field names’ with ‘field keys’ without luck…

Hello psaraph,

take a look here, perhaps it will help you …
https://russian-dima.wixsite.com/meinewebsite/rating-system

Hai Russian-Dima,

Can you check on my new post
https://www.wix.com/corvid/forum/community-discussion/changing-the-value-of-database-from-repeater

Ajith

Already done! :wink:

Thanks Russian-dima, my problem is inability to save the dataset after setfieldvalue action. I see that you included the code in main function instead of triggering it outside. I tried that and now, the error comes as “This user is not authorized to save to PPER_GROUP dataset” though I have set it as ‘read+write’ and the collection itself is set to ‘member generated content’.

So, the issue seems to be with access to the collection. Though not sure, what am I doing wrong…

Already checked the database-permissions?
Try to give full-access to your database.

The permissions are ‘member-generated content’ for collection and ‘read&write’ for dataset… the modified code based on your example is here:

//Step 3: Update the rating when the user clicks new rating
$w( ‘#GroupRating’ ).onChange(()=>{console.log( “GO” )
$w( “#dataset1” ).onReady(() => {
//Step 3.1 Find out existing ratings data from the dataset
const avg=SelectedItem.groupVitality
const count=SelectedItem.numberOfRatings
const total=SelectedItem.sumOfRatings
//Step 3.2 Register new rating value just inputted
const newRating = $w( ‘#GroupRating’ ).value
console.log( “38 Current Avg, Count and Total are” ,avg,count,total, " and New Rating is " ,newRating)
//Step 3.3 Calculate new average rating
const newAverageLong = (total + newRating) / (count + 1 );
const newAverageShort = Number.parseFloat(newAverageLong).toFixed( 1 );
//Step 3.4 Update the PEER_GROUP collection with new values
wixData.get( “PEER_GROUP” ,PG_ID)
.then((results)=>{
results.groupVitality=newAverageShort
results.numberOfRatings=count+ 1
results.sumOfRatings=total+newRating
console.log( "48 gv,num,total " ,results.groupVitality,results.numberOfRatings,results.sumOfRatings)
wixData.update(“PEER_GROUP”,PG_ID)
//- This is where I get error. Permissions for PEER_GROUP are ‘member generated content’
// and I have set #dataset1 to READ&WRITE
. catch ((err)=>{console.log( “50 Uh-oh, failed to update PEER_GROUP” )})
})
});
})
});

The permissions are ‘member-generated content’
On your place, i would first remove all database-permissions, to figure out, if this is the existing problem, or not.

I have checked if my example breaks, when i change the permissions, but is till works.

Set your DATABASE-PERMISSION to EVERYONE and check one more time.

Thanks! It works now when I have set permissions for everyone. As of ow, this should be fine, but is there a way to make this work with ‘member generated content’?