Saving ratings to database

Hi All,
Got a sticky problem,

I have created a repeater for rating local businesses (A), in the repeater I have a submit rating button (Ax) that when pressed redirects to submit rating page (B). During the redirect from A to B i use the Wix storage function to perpetuate the ID of the business the user is rating in variable ID.

Now what I’m trying to do is that when in page B, the user can submit 5 different ratings based on categories:


hence when the user presses Submit Rating the dataset connected to the page should filter based on the ID variable.

I have used the following code:

import {session} from 'wix-storage';
import wixData from 'wix-data';
let Id = session.getItem("ID")
let Business = session.getItem("Business")
let filter = wixData.filter();

$w.onReady(function () {

$w("#text34").text = Id;
$w("#text1").text = Business;

 $w("#dataset3").setFilter(
filter.eq("_id", Id));

 const currentItem = $w("#dataset3").getCurrentItem()
$w("#text35").text =  currentItem._id;
});

export function button8_click(event) {
$w("#dataset3").onReady(() => {
 // get the current item from the dataset

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

 // get the current average rating, number of ratings, and
 //total ratings for the current dataset item
 const average = currentItem.averageRating;
 const count = currentItem.submittedNumber;
 const total = currentItem.sumOfRating;

 // 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('#dataset3').setFieldValues({
 'averageRating': newAverageShort,
 'sumOfRating': total + newRating,
 'submittedNumber': (count + 1)
 });
 
 // save the dataset fields to the collection
 $w('#dataset3').save()
  .catch((err) => {
   console.log('could not save new rating');
  });
});

}

Which unfortunately is not really solving the issue, suspect it is because it requires a dynamic dataset and mine is not a dynamic page.

Any idea on how to solve this?

From my understanding I should be able to filter the data set, such that when I save the data to my dataset it filters the dataset based on ID and then adds the new data into the row?

Any help would be appreciated

Best Regards
Max

Take a look at the Ratings By User example.

Hi Yisrael, thank you for the response, I was looking for a way to ensure I didnt have to build an to advanced backend to solve this, any suggestions of other ways to solve the issue?

The example is really quite simple, it’s just that if you want to limit a user to only giving one rating on an item, then you need to deal with checking if they’re logged in or not.

A simpler Ratings example let’s visitors give a rating without checking if they’re already rated the item. However, then a user can give a thousand ratings to something that they either love or hate. Not the best way to get reliable ratings.