Hi,
I’m encountering an issue in scenario when executing a JavaScript script intended to process the values of the PRICENET and VAT fields. Despite several attempts to correct it, I keep receiving the following error:
JavaScript
JavaScript
Error: The values 'PRICENET' and 'VAT' must be defined.
Context: I have a node that needs to extract the values of PRICENET and VAT from a custom path in the received data (via parameters). These values are in string format and need to be converted to numbers for further calculations.
Encountered Issue: Although I check for the presence of these values in the script, they seem to be undefined or null, which triggers the above-mentioned error. I have tried adding handling for missing values, but the problem persists.
Here is an excerpt of the code I am currently using:
/** @CustomParams
{
"priceNetPath": {
"type": "string",
"title": "Path to PRICENET",
"description": "Specify the path to the PRICENET value from the previous node."
},
"vatPath": {
"type": "string",
"title": "Path to VAT",
"description": "Specify the path to the VAT value from the previous node."
}
}
*/
export default async function run({ execution_id, input, data }) {
// Function to convert a string to a number
function convertToNumber(value, defaultValue = 0) {
let number = parseFloat(value);
if (isNaN(number)) {
console.warn(`The value '${value}' is not a valid number, using ${defaultValue} instead.`);
return defaultValue; // Return the default value if conversion fails
}
return number;
}
// Extracting custom paths from parameters
const priceNetPath = data.priceNetPath;
const vatPath = data.vatPath;
// Extracting data using custom paths
let priceNetString = data[priceNetPath] || null;
let vatString = data[vatPath] || null;
// Log paths and values for debugging
console.log("PRICENET Path:", priceNetPath);
console.log("Raw PRICENET Value:", priceNetString);
console.log("VAT Path:", vatPath);
console.log("Raw VAT Value:", vatString);
// Check that values are not null or undefined
if (!priceNetString || !vatString) {
console.warn("The values 'PRICENET' and 'VAT' are missing or null, they will be initialized to 0.");
priceNetString = "0";
vatString = "0";
}
// Convert to number
let priceNet = convertToNumber(priceNetString);
let vat = convertToNumber(vatString);
// Display values for verification
console.log("PRICENET (number):", priceNet);
console.log("VAT (number):", vat);
// Return extracted and converted data
return { priceNet, vat };
}
My Goal: I want to ensure that these values are correctly extracted and converted to numbers to avoid errors.
I might be over killing it with this solution, so if you have a better of doing it, i’ll be happy to read you.
Thanks to anyone ready to help
Have a great one !