I am receiving the error:
UserError: An error occurred in one of undefined callbacks Caused by cannot update field values: index not found
I'm newish to coding and cannot figure out why my index can't be found in my current code. The goal of the code is to update the value of the resultField based on whether or not the fieldName (boolean) is true or false. The checkbox is attached to the fieldName and I want the resultField to be updated on the page when the checkbox is changed.
Any suggested fixes?
Here is my current test code:
$w.onReady(() => {
$w("#dynamicDataset").onReady(() => {
try {
const dataset = $w("#dynamicDataset");
const currentItem = dataset.getCurrentItem();
if (!currentItem) {
console.warn("No current item found in dataset.");
return;
}
// Log current item for debugging
console.log("Current Item from CMS:", currentItem);
// Define the array that maps dataset fields to their corresponding checkbox IDs
const checkboxMappings = [
{ fieldName: "inspiration", checkboxID: "#inspirationcheckbox"},
{ fieldName: "strengthSavingThrowProficiency", checkboxID: "#strengthcheckbox" , resultField: "strengthSavingThrow", modifier: "strength1"};
console.log("Dataset is ready.");
checkboxMappings.forEach(mapping => {
const { fieldName, checkboxID } = mapping;
const checkbox = $w(checkboxID);
if (!checkbox) {
console.warn(`Checkbox with ID ${checkboxID} not found.`);
return;
}
const value = currentItem[fieldName] || false; // Default to false
checkbox.checked = !!value;
// Debugging logs
console.log(`Checkbox ${checkboxID} set to ${!!value} for ${fieldName}`);
checkbox.onChange(() => handleCheckboxChange(dataset, mapping));
});
} catch (error) {
console.error("Error initializing checkboxes:", error);
}
});
});
function handleCheckboxChange(dataset, mapping) {
const { fieldName, checkboxID, resultField, modifier } = mapping;
if (!resultField) {
console.error("Mapping is missing resultField:", mapping);
return;
}
// const checkbox = $w(checkboxID);
// if (!checkbox) {
// console.error(`Checkbox ${checkboxID} not found.`);
// return;
// }
// const isChecked = checkbox.checked;
dataset.onReady(() => {
const currentItem = dataset.getCurrentItem();
if (!currentItem) {
console.error("No current item found in dataset. Retrying after dataset refresh...");
dataset.refresh()
.then(() => handleCheckboxChange(dataset, mapping)) // Retry after refresh
.catch(err => {
console.error("Failed to refresh dataset:", err);
});
return;
}
const isChecked = currentItem[fieldName] || false;
const baseValue = currentItem[modifier] || 0;
const proficiencyBonus = currentItem.proficiencyBonus;
const updatedValue = isChecked ? baseValue + proficiencyBonus : baseValue;
dataset.setFieldValue(resultField, updatedValue)
.then(() => {
console.log(`${resultField} successfully updated to ${updatedValue}`);
})
.catch(err => {
console.error(`Error updating field ${resultField}:`, err);
});
});
}
When I check the checkbox I receive the following error:
UserError: An error occurred in one of undefined callbacks Caused by cannot update field values: index not found Caused by: cannot update field values: index not found
When I uncheck the checkbox I receive the following error: No current item found in dataset. Retrying after dataset refresh...
UserError: An error occurred in one of undefined callbacks Caused by TypeError: Cannot read properties of undefined (reading 'then') Caused by: TypeError: Cannot read properties of undefined (reading 'then')
handleCheckboxChange()function you have a bunch of code in adataset.onReady(). That seems wrong. Thedataset.onReady()should really only be used inside the page'sonReady()handler to make sure the dataset has finished loading. Once someone is checking or unchecking a checkbox, the dataset should already be loaded and therefor thedataset.onReady()should never run.