Home > Net >  JavaScript Logic App action error "Parsing code failed. Unexpected token ...'.'."
JavaScript Logic App action error "Parsing code failed. Unexpected token ...'.'."

Time:08-22

(These are just a dummy datasets. Real datasets are big, and have 900 records in each datasets) I have two array datasets in my Logic App:

Dataset1:

[
    {
        "userId": "123",
        "name": "Victor"
    },
    {
        "userId": "456",
        "name": "Jack"
    },
    {
        "userId": "789",
        "name": "Winston"
    }
]

Datset2:

[
    {
        "userId": "123",
        "age": "75"
    },
    {
        "userId": "456",
        "age": "72"
    }
]

I want to apply a left join in Logic App and generate a final output:

    {
        "userId": "123",
        "name": "Victor",
        "age": "75"
    },
    {
        "userId": "456",
        "name": "Jack",
        "age": "72"
    },
    {
        "userId": "789",
        "name": "Winston",
        "age": null
    }
]

First I applied a For Each loop and condition using native Logic App actions, which works fine but since the datasets are large, it takes around 3 hours to complete. So I applied a below Inline JavaScript code in Logic App.

const dataset1 = [
    {
        "userId": "123",
        "name": "Victor"
    },
    {
        "userId": "456",
        "name": "Jack"
    },
    {
        "userId": "789",
        "name": "Winston"
    }
]

const dataset2 = [
    {
        "userId": "123",
        "age": "75"
    },
    {
        "userId": "456",
        "age": "72"
    }
]

const output = dataset1.map(x => {
    const result = dataset2.find(element => element.userId === x.userId);
    return {...x, age: result?.age || null};
})

console.log(output)

When I run this code outside of Logic App, it works fine but when I run it from the inside the Logic App JavaScript action, it does not let me save the Logic App and throws below error:

enter image description here

Failed to save logic app logic-test. 'The input parameter 'code' for inline code action 'Execute_JavaScript_Code' contains invalid code. Parsing the code failed with error 'Line 29: Unexpected token ...'.'. Status code: 'BadRequest'.

CodePudding user response:

try this

return result === undefined ? { ...x, age: "null" } : { ...x, ...result };

but dont give me a negative point i'm trying to help

CodePudding user response:

i think you just forget parenthesis in you inline function

 const output = dataset1.map((x) => {
 const result = dataset2.find((element) => element.userId === x.userId);
 return { ...x, age: result?.age || null };
 });

result:

enter image description here

  • Related