Problem in IF node

Hello,

I started using n8n today to create an ETL process.
I need to get information from a web server and compare if it exists in my postgres database. For that I create this workflow.

in the IF node, I added the following expression:

$node["ClientesCX"].json["data"].includes('$node["Postgres"].json["external_id_client"]')

image

After each execution, it always falls to false, even though there is a value in my array

The data of the ClienteCX node are:

[ "12877", "1337", "1338", "13553", "14113", "14315", "14541", "14973", "15139", "15718", "16806", "16987", "17170", "17202", "17223", "18635", "18660", "18824", "18903", "19239" ]

The data of Postgres node are:

[
  {
    "external_id_client": 13579
  },
  {
    "external_id_client": 16536
  },
  {
    "external_id_client": 12504
  },
  {
    "external_id_client": 12011
  },
  {
    "external_id_client": 12011
  }
]

I try using .toString(), but i have the same problem.

Can you help-me?
OBRIGADO!

Edit: In the false execution i send a new http request

the code of the workflow is:

Try to compare strings. This usually works for me.

This way:

{{$node["ClientesCX"].json["data"].includes($node["Postgres"].json["external_id_client"]) + ""}}

and then add the string true as a second parameter in the IF node.

Hope this helps.

I change to:

{{$node["ClientesCX"].json["data"].join().includes($node["Postgres"].json["external_id_client"]) ? "true" : "false"}}

now, in the first run, it returns TRUE, in the other FALSE, even though it exists in the comparison array.

image

Any suggestion?

EDIT

I used SET node to be able to view the values ​​of the ClientCX node, in the first execution, it populated the field as expected, in the others the field was not created.
How can I do to “freeze” the values ​​of the ClientCX node, to use in other executions?

After run, this is the result:
image

the field cxCli is using the values from CXClient node, the field properyName is using the value from actual node execution (Postgres node)

Hi @jurasseck,

To simplify, I recommend you to create a workflow with the minimum steps (GetClientesCX - forcing token -, ClientesCS and Postgres).

Probably, you will need to return an empty response in Postgres node when nothing is found (settings tab in the node).

This way you will be able to reduce complexity and test the small workflow.

I recommend you to create a Function Item node after Postgres to calculate what Clientes exist or not.

@Miquel_Colomer

I created the workflow, but i cant catch data from ClientCX node.

When i run, i receive the error:
ERROR: No data found for item-index: "1"

Can u help me?

Workflow:

Postgres Data Result:

[
{
"external_id_client": 12877
},
{
"external_id_client": 13553
},
{
"external_id_client": 12877
},
{
"external_id_client": 14113
},
{
"external_id_client": 14315
}
]

OBRIGADO!

Hi @jurasseck,

Sorry but I am not able to reproduce the error.
Have you shared the exact workflow?

By the way, I have created a workflow that checks data array returned by ClienteCX against all items returned by Postgres faked data, using includes function.

Check it please:

{
  "name": "test_n8n",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "var items = [\n  {\n    \"external_id_client\": 13579\n  },\n  {\n    \"external_id_client\": 16536\n  },\n  {\n    \"external_id_client\": 12504\n  },\n  {\n    \"external_id_client\": 12011\n  },\n  {\n    \"external_id_client\": 12011\n  }\n]\n\n\nreturn items.map(function(item) {\n  return {\n    json: {\n      external_id_client: item.external_id_client,\n      found: $node['Map ClientesCX Ids'].json.data.includes( item.external_id_client)\n    }    \n  }\n});"
      },
      "name": "Map Postgres Ids",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "var ids = [ \"12877\", \"1337\", \"1338\", \"13553\", \"14113\", \"14315\", \"14541\", \"14973\", \"15139\", \"15718\", \"16806\", \"16987\", \"17170\", \"17202\", \"17223\", \"18635\", \"18660\", \"18824\", \"18903\", \"19239\" ]\n\n\nreturn [\n  {\n    json: {\n      data: ids\n    }\n  }\n]"
      },
      "name": "Map ClientesCX Ids",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "executeOnce": false,
      "alwaysOutputData": true
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Map ClientesCX Ids",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Postgres Ids": {
      "main": [
        []
      ]
    },
    "Map ClientesCX Ids": {
      "main": [
        [
          {
            "node": "Map Postgres Ids",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "179"
}

The most important thing is to understand how workflow execution works inside n8n.
Sometimes this is a bit cryptic.

Perhaps somebody knows it better but I am not sure if you can access to step data from future steps inside a SplitInBatches subprocess.

I usually change flow strategy in those cases and I prefer to recover ClientesCX data outside of SplitInBatches subprocess.

Another option could be using Method: getWorkflowStaticData(type)

It allows to save data globally in a first step, and recover variable to use it later in future steps. Perhaps this suits your needs doing minimal changes in your flow.

1 Like

@Miquel_Colomer

Now it’s working =D.
I catch the postgres data, and map using a data from Clients creating a new field to use in IF node.

Thank u for the help

1 Like

You are welcome!

Enjoy n8n!