String To Number conversion headache

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 :smile:
Have a great one !

Hi,

First, check the JavaScript inputs to determine whether the issue lies in how the variables are being handled or if they are undefined even before reaching the JavaScript.

Overall, it’s quite an interesting issue—let’s dig into it and figure this out!

Could you explain in what format the data is received, please? I’ll run some tests, and we can figure this out.

Hi Raian, thanks for the quick help. :metal:

The data is received in JSON format via a webhook.
Regarding the JS module, It includes two main fields: service_id_input and calendar_id_input. Each field contains a string representing the respective IDs

Here are the entry data of my scenario :

 "COMMAND": "CREATED",
        "APPOINTMENTUID": "b9033e9d-6787-4598-8f1e-b2793f1b44ab",
        "STARTDATETIMEUTC": "20240928 134500",
        "ENDDATETIMEUTC": "20240928 150000",
        "STARTDATETIME": "20240928 154500",
        "ENDDATETIME": "20240928 170000",
        "STARTDATE": "2024-09-28",
        "ENDDATE": "2024-09-28",
        "BOOKINGDATEUTC": "20240927 124108",
        "SALUTATION": "",
        "LASTNAME": "Casazza",
        "FIRSTNAME": "Aurélie",
        "EMAIL": "**************",
        "PHONE": "***********",
        "STREET": "",
        "ZIP": "",
        "TOWN": "",
        "BIRTHDAY": "",
        "NOTES": "",
        "CUSTOMERNUMBER": "",
        "SELECTEDANSWERS": "Massage 60'",
        "SERVICEID": "436286",
        "BOOKINGLANGUAGE": "fr",
        "CALENDARNAME": "Salle presta",
        "CALENDARID": "162274",
        "CALENDARID2": "",
        "STATE": "",
        "CAPACITY": "1",
        "APPATTRIB": "0",
        "VOUCHERCODE": "",
        "PRICENET": "5833",
        "VAT": "1167",
        "PRICEGROSS": "7000",
        "LINKEDAPPIDS": "",
        "PRICEGROSS100": "70",
        "REFERENCECODE": "B9033E9D",
        "NEWSLETTER": "false",
        "REASON": "",
        "ID": "100157672",
        "CONTACTID": "30877836",
        "EXTERNALIDOLD": "",
        "USERID": "Internet",
        "TITLE": ""
    }

Try using our AI node; it will help transform the data the way you need. Give it a try—I got it to work perfectly with your data on the first attempt! It’s an excellent assistant for any automation tasks.

Write to the AI node like this:

I need to set up data processing that I receive via a webhook. Specifically, I need to:

Extract the values of PRICENET and VAT from the webhook data (node 1).
These values are coming as strings, and I need to convert them to numbers.
If the data is missing (for example, if they are null or absent), I want them to automatically be set to 0.
In the end, I want to return PRICENET and VAT as numbers for further calculations.

1 Like

Wow!
Thanks a lot!
I tried using the AI module but without success.
Clearly, I was doing it wrong.
Any advice on how to structure a proper prompt for Latenode’s AI?
And how did you make such a great video so fast ?? (So much wizardry! yes…i’m new arround here :wink: )

Again, thank you ! you’ve made my day :sun_with_face:

1 Like

Glad to be of help!

To use the AI node correctly, always specify the node numbers and clarify which node you want to process. Also, try to describe your request in as much detail as possible (like in my example).

As for recording videos, if you’re using a Mac, the app Screen Studio (it costs about $40) is a great option. If you’re on Windows, I recorded with FocuSee, which officially costs around $60 but is always available on AppSumo for $37-38. It’s an excellent tool—the best screen recorder for windows I’ve come across.

1 Like

Thanks for all these info.

I’m still having issues.
And I’m thinking that the problem isn’t where I thought it was.

can't parse integer value in custom field: strconv.Atoi: parsing "0.00": invalid syntax

These numbers are supposed to be sent to an Airtable module.
It seems to work if I use a round() function.
But I can’t use decimal numbers with this Airtable module.
The field in which these numbers are sent is a € field with two decimals, i just don’t get it.

I know I’m missing something here… but what?
Any guesses?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.