Question:
I accidently synced my sandbox to the live data, and now the code no-longer works. I can’t see any obvious reasons why this has happened.
Product:
Wix Editor
What are you trying to achieve:
I would like the code to work again and the results be listed in the repeater.
What have you already tried:
I’ve checked the live data, and the ID’s are the same as before and checked help guides
Additional information:
This is the code:
// Import necessary WIX modules
import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
$w.onReady(function () {
const startYear = 2000;
const endYear = new Date().getFullYear();
const decades = ;
// Generate list of decades
for (let year = startYear; year <= endYear; year += 10) {
let decadeEnd = Math.min(year + 9, endYear);
decades.push({
_id: `${year}-${decadeEnd}`, // Unique identifier for each decade
title: `${year}-${decadeEnd}`
});
}
// Log the generated decades to ensure they are correct
console.log("Decades generated:", decades);
// Check if the decadeRepeater element is available
if ($w('#decadeRepeater')) {
// Populate the repeater with the list of decades
$w('#decadeRepeater').data = decades;
$w('#decadeRepeater').onItemReady(($item, itemData) => {
// Log itemData to ensure it's correct
console.log("Item data:", itemData);
// Ensure the decadeButton and decadeText elements are available
if ($item('#decadeButton') && $item('#decadeText')) {
// Set the text for the button and text elements
$item('#decadeButton').label = itemData.title;
$item('#decadeText').text = itemData.title;
// Ensure the button click event is set correctly
$item('#decadeButton').onClick(() => showYears(itemData.title));
} else {
console.error("Decade button or text element not found in the repeater item.");
}
});
} else {
console.error("Decade repeater not found");
}
});
// Function to show years within the selected decade
function showYears(decade) {
let [startYear, endYear] = decade.split(‘-’).map(Number);
const years = ;
for (let year = startYear; year <= endYear; year++) {
years.push({
_id: year.toString(), // Unique identifier for each year
title: year.toString()
});
}
// Check if the yearRepeater element is available
if ($w('#yearRepeater')) {
$w('#yearRepeater').data = years;
$w('#yearRepeater').onItemReady(($item, itemData) => {
$item('#yearButton').label = itemData.title;
$item('#yearText').text = itemData.title;
$item('#yearButton').onClick(() => showYearData(itemData.title));
});
$w('#decadeRepeater').collapse();
$w('#yearRepeater').expand();
} else {
console.error("Year repeater not found");
}
}
// Function to show data for the selected year
function showYearData(year) {
// Log to ensure the function is being called with the correct year
console.log(“Showing data for year:”, year);
wixData.query('repository')
.eq('date', parseInt(year)) // Ensure date is queried as a number
.find()
.then(results => {
// Log the results to ensure the query is returning the correct data
console.log("Results from repository for year", year, ":", results.items);
if (results.items.length > 0) {
const items = results.items.map(item => ({
_id: item._id, // Use the existing unique identifier from the repository
title: `Data for ${year}`,
// Add other fields from your CMS if necessary
data: item
}));
// Check if the dataRepeater element is available
if ($w('#dataRepeater')) {
$w('#dataRepeater').data = items;
$w('#dataRepeater').expand();
} else {
console.error("Data repeater not found");
}
} else {
console.log(`No data found for year ${year}`);
// Clear data repeater if no results found
if ($w('#dataRepeater')) {
$w('#dataRepeater').data = [];
$w('#dataRepeater').collapse(); // Optional: Hide repeater if no data
}
}
})
.catch(err => {
console.error("Error querying repository for year", year, ":", err);
});
$w('#yearRepeater').collapse();
}