- 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:
-
App json export: (here's how to get a json export)