Static variable value issue in a loop

  • Goal:
    Im' having an issue with my transformer code where the variable value doesn't change in a loop.
    I've never had this issue before but the variable.value adopts the value of the first loop and doesn't change in the following loops

Edit: To explain a bit better , the variable[columnName] which refers to {{ valueTransformer.value[0] }} ( a variable within the app - of type array ) collects the inputs from a user to change the row of an imported file. However when a field is referenced between '{' and '}' in the input string, the function UserModifiedValues shown below would turn the string into the value referenced from file data.
However said value should be different for each row, but logs show that declared 'variable' does not change in the forEach loop and value is duplicated for each processed row. I don't understand what causes this maybe a closure issue ?

here is the portion of the code causing the issue

const variable = {{ valueTransformer.value[0] }};
let result;

if ({{ fileDropzone1.value[0].type }} == "text/csv") {
    // If the file type is CSV, access the value directly
    result = {{ fileDropzone1.parsedValue[0] }};
} else {
    // If the file type is not CSV, access the value based on selectedIndex
    result = {{ fileDropzone1.parsedValue[0][select11.selectedItem] }};
}

const mappedData = result.map((row, i) => {
    const mappedRow = {};
    Object.keys({{ columnMapping.value[0] }}).forEach(columnName => {
        const databaseField = {{ columnMapping.value[0] }}[columnName]

        if (row.hasOwnProperty(databaseField)) {
            let modifiedValue = row[databaseField];
            mappedRow[columnName] = modifiedValue;
        } else if (Object.keys(row).indexOf(databaseField) == -1) {
            let modifiedValue = "";
            mappedRow[columnName] = modifiedValue;
        }               
        
        if (Array.isArray(variable[columnName])) {
  mappedRow[columnName]= variable[columnName].map((item) => {
                if (item.value != undefined) {
                 
                    item.value = UserModifiedValues(item.value);
console.log(item);                   
 return item
                } else if (row[{{ columnMapping.value[0].ID }}] == item.id) {
                    mappedRow[columnName] = UserModifiedValues(item.content);
                } else if (item.id == "") {
                    try {
                        mappedRow[columnName] = UserModifiedValues(item.content);
                    } catch (error) {
                        console.log(error.message);
                    }
                }
            });
}

the user modifie funtion is the following 

function UserModifiedValues(userInput) {
const varRegex = new RegExp("{(.*?)}", "g");

    try {
        const processedText = userInput.replace(varRegex, (match, varName) => {
            const varValue = row[varName.trim()];
            return varValue !== undefined ? varValue : match;
        });
        return processedText;
    } catch (e) {
        console.log(e.message);
    }
}
  • Steps:
    Apparently the function works when it's not in a array, but always returns the same value for every processed row

  • Details:

  • Screenshots:
    image

  • App json export: (here's how to get a json export)

1 Like

Is {{ valueTransformer.value[0] }} an array (the first array of arrays...)? I'm not fully grasping your code...you keep referencing item [0] throughout - is that meant to move, or is columnMapping.value[0] another array (the first array in an array of arrays)?

Hello yes to answer your question {{valueTransformer.value[0]}} is an array that looks like this :
[{
name : {
{id: "productID", content:""}
{id: "productID", content:""}
},...
}]
same for columnMapping which references column titles of the file and their corresponding key as string. Technically it should not move its always the first array of arrays that is being evaluated.

Hi @Ecommfox,

An app export might help here! The sample code for {{valueTransformer.value[0]}} looks like it might be missing some data since it throws an error -

To share an export: