Onitemready function not running. Why not?

Hello! I’m having a little bit of trouble with some of my code and I’m hoping someone out there may be able to help me out. I’ve looked through the forum and haven’t been able to find a similar issue, so I apologize if I missed someone else’s answer to my problem.

So, I’m attempting to use Wix’s “Gift Quiz” (ie a recommended products quiz) that they have a video and a few posts on. I believe that I have worked my way through all of the other problems with the quiz (included some code errors that were mistakenly left in by the writer, it seems), but have arrived at one that I just can’t seem to figure out! For those not familiar with it, it’s basically a slideshow with buttons, that advances slides when you press one of said buttons. When you reach the end of the “quiz”, it displays a results page based on which buttons your pressed. My problem is that the repeater in the final slide of the quiz just won’t show any of my products or information, although it does load in with the default placeholder text, pictures, and such.

I believe I have found the main issue, but I’m not sure how to fix it. Based on my amateur level deduction, it seems that, although I have an “onitemready” event handle on the repeater, it won’t properly run the function when the user reaches the final slide of the slideshow, which holds the repeater. I determined this by adding the console.log (“It works”), and running a preview. However, “It works” never shows up in the log, leading me to believe that the function isn’t running in the first place. Please note that the repeater is embedded in the last slide of a slideshow, so this may have something to do with the problem.

I expect that once the user reaches the last slide, the repeater should load and trigger the “onitemready” once it is finished, and this should run the following code (which of course, doesn’t happen). The very last section of code is as follows (I’ll post the entire code at the end):

export function recommendedproducts_itemReady($item, itemData, index) {
  $item('#name').text = itemData.name;
  $item('#image').src = itemData.mainMedia;
  $item('#image').link = itemData.productPageUrl;
  $item('#price').text = itemData.formattedPrice;
  console.log("It works");
}

As I mentioned, although the default repeater is displayed, none of the respective parts of the repeater are replaced with this new information. “It works” does not show up in the console log, either.

Thank you very much for taking the time to read through this! I would greatly appreciate any help! I’ll post the entire code here:

import wixData from 'wix-data';

const selectedAnswers = [];
const quizAnswersIds = ["80s","90s","00s","10s","current","collect","hang","both","low","medium","high","veryhigh","nolimit"];

let numberOfSlides;

$w.onReady(function () {

  numberOfSlides = $w('#quizSlides').slides.length;
 
  quizAnswersIds.forEach(answer => {
    $w(`#${answer}`).onClick (() => {
      selectedAnswers.push(answer);
      $w('#quizSlides').next();
 })
 })
});

export async function quizSlides_change(event) {
 if (isQuizFinished()) {
 let quizProducts = await getKeywordsPerProduct();
    selectedAnswers.forEach(answer => {
      quizProducts = filterProductsPerAnswer (quizProducts, answer)
 });
    quizProducts = quizProducts.map(quizProduct => quizProduct.productId);
    quizProducts = getRandomItemsFromArray (quizProducts, numberOfSlides)
    $w("#recommendedproducts").data = await getProductsData(quizProducts);
 }
}

function isQuizFinished(){
 return $w('#quizSlides').currentIndex === numberOfSlides - 1;
}

async function getKeywordsPerProduct() {
 let ProductKeywords = await wixData.query("ProductKeywords").find();
 ProductKeywords = ProductKeywords.items;
 ProductKeywords = ProductKeywords.map(item => {
 return {
      productId: item.product,
      keywords: item.keywords.split(",")
 }
 });
 return ProductKeywords;
}

function filterProductsPerAnswer(quizProducts,answer){
 const filteredProducts=quizProducts.filter(quizProduct => {
 return quizProduct.keywords.includes(answer)
 });
 return filteredProducts;
}

function getRandomItemsFromArray(productsArr, numberOfItems){
 const productsIds = [];
 let numberOfProducts = productsArr.length;

 for (let i=0; i<numberOfItems && i<numberOfProducts; i++) {
 const randomIndex = getRandomInt(0,numberOfProducts - 1);
    productsIds.push(productsArr[randomIndex]);
    productsArr.splice(randomIndex,1);
 }
 return productsIds;
}

function getRandomInt(min,max) {
 return Math.floor(Math.random() + (max - min +1) + min);
}

async function getProductsData(productsIds) {
 const productsData = await wixData.query("Stores/Products")
 .hasSome("_id", productsIds)
 .find();
 return productsData.items;
}

export function recommendedproducts_itemReady($item, itemData, index) {
  $item("#name").text = itemData.name;
  $item('#image').src = itemData.mainMedia;
  $item('#image').link = itemData.productPageUrl;
  $item('#price').text = itemData.formattedPrice;
  console.log("It works");
}

This last function called recommendedproducts_itemReady() is an event handler created when you select the repeater, click in properties and click on the event to create such function. Over time, this connections between element and event handler breaks, or if you copied the code you didn’t created in the first place. Can you check if the event handler is connected to the code?

I strongly advise to not use this kind of connections, cause they cause to many troubles over time. You coud just call the function directly from the element, like this:

$w("#recommendedproducts").onItemReady(($item, itemData, index) => {
    $item("#name").text = itemData.name
    $item("#image").src = itemData.mainMedia
    $item("#image").link = itemData.productPageUrl
    $item("#price").text = itemData.formattedPrice
    console.log("It works")
})

Remember, if you use one way, you cannot use the other.

Hello Bruno! Thank you very much for taking the time to help me with this. And also the screenshots, as I’m sure that took some extra time and effort.

I did have the event handler “onItemReady()” linked to the code “recommendedproducts_itemReady()”, which unfortunately didn’t work.

To be honest, I’m not so familiar with using event handlers, so I much prefer your suggestion of not using them at all. The only reason I had used it in the first place was that I basically copied this code from a wix article.

So, I tried your suggestion, but it didn’t seem to solve the problem. I was sure to remove the event handler and then carefully insert the code you recommended. I also added an extra console.log outside of this section, just to make sure everything else was working as it should:

$w("#recommendedproducts").onItemReady(($item, itemData, index) => {
    $item("#name").text = itemData.name
    $item("#image").src = itemData.mainMedia
    $item("#image").link = itemData.productPageUrl
    $item("#price").text = itemData.formattedPrice
    console.log("It works")
}
)
console.log("It works on the outside")

Unfortunately, it seems to suffer from the same problem as before. T_T

I saw another post that mentioned issues with repeaters in slideshows:

They said that trying to popular a repeater in a slideshow only works if the slide with the repeater is in view. So, I moved last slide to the front so that it would be the first to show up, but this didn’t seem to fix the problem either.

It seems to my inexperienced coding knowledge that this may be more of a platform issue than it is a coding issue. What are your thoughts?

Thanks again for your time to help me with this problem!

Can you do me a favor and put the console.log() at the function that generates the data to the repeater?

async function getProductsData(productsIds) {
 const productsData = await wixData.query("Stores/Products").hasSome("_id", productsIds).find()
    console.log(productsData) //This
 return productsData.items
}

Hello Bruno! Forgive me for not replying to you sooner!

Your comment was spot on! I put the console.log in the function and found that it was never running in the first place. So I put console logs in the other functions and found that none, except one, was even running! And so I’ve since been spiraling down this rabbit hole of rewriting it without functions!

Since then, I haven’t come across any issues that I wasn’t able to solve on my own after a little bit of research, which is why I hadn’t replied. I just wanted to make sure that I let you that your help was greatly appreciated and definitely helped me get closer to completing this code, so thank you very much!