I wrote a code to set the average score for each item in my repeater from a reviews dataset. The code works perfectly in the preview mode, but not in the published site. I need help with this because I do not understand what happens. By the way, datasets I use in my code have no restrictions (they are not set as read-only). My code below:
import wixData from 'wix-data';
import wixWindow from 'wix-window';
$w.onReady(async function() {
// Get service availability to populate in the toggle.
$w('#serviceRepeater').onItemReady(async ($item) => {
// Populate the review score per psychologist.
await wixData.query('Bookings/Services')
.eq('serviceName', $item('#serviceName').text) // Query the serviceName to find Id.
.find()
.then(async (result) => {
let psyId = result.items[0]._id;
wixData.query('SubmitReviews') // Return/await ._id from Services to find psychologist reviews.
.eq('medico', psyId)
.include('medico') // Include referenced field in the search with ._id.
.find()
.then(async (results) => {
let sum = 0;
if(results.items.length > 0) {
for(let i = 0; i < results.items.length; i++) {
sum = sum + results.items[i].calificacion;
}
let rating = (sum / results.totalCount).toFixed(1); // Declare rating and calculate score average to fixed(1).
$item('#reviewText').text = rating;
console.log(rating);
}
})
.catch((error) => {
console.log(error)
})
})
.catch((error) => {
console.log(error)
})
// Open a lightbox with more information about psychologist.
$item('#psycoBoxButton').onClick(() => { // If user press more information button.
wixWindow.openLightbox('Psychology Box', { // Open the Psychology lightbox.
'psyNameSend': $item('#serviceName').text // Create an object with psychologist name from the imtem's repeater.
})
})
})
})