Hi, I'm trying to create a gift quiz that recommends site products based on results. The quiz is flowing, but when it gets to the result page, it is still showing the default repeater. Wix is showing me two error messages within these pieces of code below, and I'm not sure if that has anything to do with it. I connected the results slide to the dataset to see if that would make a difference, but it is showing me all of the products except the ones with the correct keywords. Please help!
export async function quizSlides_change(event) { // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4 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 productsKeywords = await wixData.query("productsKeywords").find (); productsKeywords = productsKeywords.items; productsKeywords = productsKeywords.map(item => { return { productId: item.product, keywords: item.keywords.split(".") } }); return productsKeywords; }
@Imani Gabriel, You wrote, "two error messages within these pieces of code"...
What error messages? On what lines of code?
Alright.
The first thing to check is that all of the event handlers that are connec
ted via the UI are setup correctly. So the recommendedProducts repeater should have this recommendedProducts_itemReady function attached as its onItemReady event handler. That event handler triggers for each of the repeater items whenever they get new data.
If that's already setup, you can try console.logging the results of await getProductsData(quizProducts), to make sure that the data being sent back there is valid. If the repeater isn't updating at all, there's a chance that the array of products that's being returned by that call does not have an "id" field for each item. Let me know if either of these works
This is the rest of the code I have. The bottom is what I have to display the product on the repeater "recommendedProducts."
function filterProductsPerAnswer(quizProducts,answer){ const filteredProducts = quizProducts.filter(quizProduct => { return quizProduct.keywords.includes(answer) }); return filteredProducts; } function getRandomItemsFromArray(productsArr,numberOfItems){ const productIds = []; let numberOfProducts = productsArr.length; for (let i = 0;i < numberOfItems && i < numberOfProducts;i++){ const randomIndex = getRandomInt(0,productsArr.length - 1); productIds.push(productsArr[randomIndex]); productsArr.splice(randomIndex, 1); } return productIds; } 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; }
Hey Imani,
To better help, have you written the function to handle the styling of the results repeater when it gets the new data?