Hello,
I need some help, I've been working on this for a couple of days now. I have fixed some errors but I can't seem to get over the the below.
My rating and reviews for my dynamic page is not displaying, when the information is sent to the light box and I hit submit review these two error codes show:
Hook beforeGet for collection Members result ignored! Expected hook result to resolve to a string, but got [Object]
An error occurred in one of afterSave callbacks Error: ItemId must be a string.
I do understand what a string is but I can't seem to fix the issue. I have followed an article on here and replaced the product ID with the ID I would like to reference which is the Business name title.
below is my lightbox code:
//-------------Imports-------------//
import wixWindow from 'wix-window';
import wixData from 'wix-data';
//-------------Global Variables-------------//
// Current product's ID.
let businessName;
//-------------Lightbox Setup-------------//
$w.onReady(function () {
// Get the data passed by the page that opened the lightbox.
businessName = wixWindow.lightbox.getContext();
// Set the action that occurs before the review is saved.
$w('#submitReviews').onBeforeSave(() => {
// If no rating was set:
if ($w('#radioRating').value === '') {
// Display an error message.
$w('#rateError').show();
// Force the save to fail.
return Promise.reject();
}
// If a rating was set, set the element values into the fields of the dataset item.
// These values will be saved in the collection.
$w('#submitReviews').setFieldValues({
businessName,
rating: $w('#radioRating').value,
recommends: $w('#radioGroup1').value
});
});
// Set the action that occurs after the review is saved.
$w('#submitReviews').onAfterSave(async () => {
// Update the product's statistics using the updateStatistics() function.
await updateStatistics($w('#radioGroup1').value);
// When the statistics have been updated, close the lightbox to return the user to the product page.
wixWindow.lightbox.close();
});
});
// Update (or create) the product statistics.
async function updateStatistics(isRecommended) {
// Get the review statistics for the current product from the "reviewStats" collection.
let stats = await wixData.get('reviewStats', businessName);
// If statistics data already exist for this product:
if (stats) {
// Add the new rating to the total rating points.
stats.rating += parseInt($w('#radioRating').value, 10);
// Increase the ratings count by 1.
stats.count += 1;
// Increase the recommendations by one if the user recommends the product.
stats.recommended += (isRecommended === "true") ? 1 : 0;
// Update the new product statistics in the "reviewStats" collection.
return wixData.update('reviewStats', stats)
}
//If no statistics data exists for this product, create a new statistics item.
stats = {
// Set the statistics item's ID to the current product's ID.
_id: businessName,
// Set the statistics item's rating to the rating entered by the user.
rating: parseInt($w('#radioRating').value, 10),
// Set the statistics item's ratings count to 1 because this is the first rating.
count: 1,
// Set the statistics item's recommended property to 1 if the user recommends the product.
recommended: (isRecommended === "true") ? 1 : 0
};
// Insert the new product statistics item into the "reviewStats" collection.
return wixData.insert('reviewStats', stats)
}
//-------------Event Handlers-------------//
// Set the action that occurs when a rating is chosen.
export function radioRating_change(event, $w) {
// Hide the error message.
$w('#rateError').hide();
}