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:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        110,
        -40
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=select \n sic.id_cliente as external_id_client\n\nfrom\n    shop_informacoes_clientes sic\n    inner join shop_contratos sc on sc.id_cliente = sic.id_cliente\nwhere 1=1\n    and sc.id_empresa = {{$node[\"SplitInBatches\"].json[\"operacaoId\"]}}\n    and sc.mastercto is null\nlimit 5"
      },
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        910,
        40
      ],
      "credentials": {
        "postgres": "Universo Digital"
      }
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        320,
        110
      ],
      "notesInFlow": true,
      "notes": "Quebra a execução por batchs para cada operação."
    },
    {
      "parameters": {
        "functionCode": "return [\n  /*{\n    json: {\n      operacao: '50',\n      key: 12314\n    }\n  },\n  {\n    json: {\n      operacao: '51',\n      key: 45454545454\n    }\n  },\n  {\n    json: {\n      operacao: 'plugmobile',\n      operacaoId: 33,\n      token: '742ad19e129b7fe49ab84a2bb886f084'\n    }\n  },*/\n  {\n    json: {\n      operacao: 'gerenciadores',\n      operacaoId: 51,\n      token: '742ad19e129b7fe49ab84a2bb886f084'\n    }\n  }\n];\n \n"
      },
      "name": "Informações das operações",
      "type": "n8n-nodes-base.function",
      "position": [
        120,
        170
      ],
      "typeVersion": 1,
      "notesInFlow": true,
      "notes": "Operações:\nComponentes: 50\nGerenciadores: 51\nPlugmobile: 33"
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"ClientesCX\"].json[\"data\"].includes('$node[\"Postgres\"].json[\"external_id_client\"]')}}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1080,
        40
      ]
    },
    {
      "parameters": {
        "webhookUri": "https://discord.com/api/webhooks/806138234164740176/GoOmlrcuF4TaWYE18FtQe3f3OpMfQ9AeNfb7a-3iw3dtIQjALs6BMe5r-syUHkJ6HCST",
        "text": "=Esse cara já existe: {{$node[\"Postgres\"].json[\"external_id_client\"]}}"
      },
      "name": "Discord",
      "type": "n8n-nodes-base.discord",
      "typeVersion": 1,
      "position": [
        1260,
        -70
      ]
    },
    {
      "parameters": {
        "webhookUri": "https://discord.com/api/webhooks/806138234164740176/GoOmlrcuF4TaWYE18FtQe3f3OpMfQ9AeNfb7a-3iw3dtIQjALs6BMe5r-syUHkJ6HCST",
        "text": "=Esse cara FOI CRIADO: {{$node[\"Postgres\"].json[\"external_id_client\"]}}"
      },
      "name": "Discord1",
      "type": "n8n-nodes-base.discord",
      "typeVersion": 1,
      "position": [
        1260,
        170
      ]
    },
    {
      "parameters": {
        "functionCode": "const payload = items[0].json.map(item => {\n  return {\n    json: { external_id_client: item.external_id_client }\n  }\n});\n\nreturn [\n  {\n    json: {\n      data: payload.map(data => data.json.external_id_client)\n    }\n  }\n]\n"
      },
      "name": "ClientesCX",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        710,
        40
      ]
    },
    {
      "parameters": {
        "url": "https://sandbox.api.customerx.com.br/api/v1/clients",
        "options": {},
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Authorization",
              "value": "={{$node[\"SplitInBatches\"].json[\"token\"]}}"
            },
            {
              "name": "Content-type",
              "value": "application/json"
            }
          ]
        }
      },
      "name": "GetClientesCX",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        530,
        40
      ],
      "notesInFlow": true,
      "executeOnce": false,
      "notes": "Clientes existentes no CX"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Informações das operações",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "GetClientesCX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Informações das operações": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF1": {
      "main": [
        [
          {
            "node": "Discord",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Discord1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Discord": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Discord1": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ClientesCX": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetClientesCX": {
      "main": [
        [
          {
            "node": "ClientesCX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        110,
        -40
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=select \n sic.id_cliente as external_id_client\nfrom\n    shop_informacoes_clientes sic\n    inner join shop_contratos sc on sc.id_cliente = sic.id_cliente\nwhere 1=1\n    and sc.id_empresa = 51\n    and sc.mastercto is null\n\nlimit 5"
      },
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        740,
        -40
      ],
      "alwaysOutputData": true,
      "credentials": {
        "postgres": "Universo Digital"
      }
    },
    {
      "parameters": {
        "functionCode": "const payload = items[0].json.map(item => {\n  return {\n    json: { external_id_client: item.external_id_client }\n  }\n});\n\nreturn [\n  {\n    json: {\n      data: payload.map(data => data.json.external_id_client)\n    }\n  }\n]\n\n"
      },
      "name": "ClientesCX",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        -40
      ],
      "executeOnce": false,
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "url": "https://sandbox.api.customerx.com.br/api/v1/clients",
        "options": {},
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Authorization",
              "value": "=742ad19e129b7fe49ab84a2bb886f084"
            },
            {
              "name": "Content-type",
              "value": "application/json"
            }
          ]
        }
      },
      "name": "GetClientesCX",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        350,
        -40
      ],
      "notesInFlow": true,
      "executeOnce": false,
      "notes": "Clientes existentes no CX"
    },
    {
      "parameters": {
        "functionCode": "item.cx = $node[\"ClientesCX\"].data;\n\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        940,
        -40
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "GetClientesCX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ClientesCX": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetClientesCX": {
      "main": [
        [
          {
            "node": "ClientesCX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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!