IF node misbehaving in specific cases

Describe the issue/error/question

“If node” doesn’t route properly on the output.
it triggers both false and true output when its preceded by HTML Extract node.

The below screenshot is the part of the workflow where the issue happens.

The workflow is way too big and contains sensitive data so I’ll just share that part.

{
  "nodes": [
    {
      "parameters": {
        "extractionValues": {
          "values": [
            {
              "key": "image",
              "cssSelector": "img[data-src]",
              "returnValue": "attribute",
              "attribute": "data-src",
              "returnArray": true
            },
            {
              "key": "css",
              "cssSelector": "link[rel=\"stylesheet\"][type=\"text/css\"][href$=\"/css/style.css\"]",
              "returnValue": "attribute",
              "attribute": "href"
            }
          ]
        },
        "options": {}
      },
      "name": "HTML Extract1",
      "type": "n8n-nodes-base.htmlExtract",
      "typeVersion": 1,
      "position": [
        340,
        800
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"css\"]}}",
              "operation": "contains",
              "value2": ".css"
            }
          ],
          "boolean": [
            {
              "value1": "={{$json[\"image\"].length === 0 }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF2",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        520,
        800
      ]
    },
    {
      "parameters": {
        "functionCode": "var array =[];\n\nfor (var item_arr of items) {\n  if (item_arr.json.image) {\n    for (var item of item_arr.json.image) {\n      array.push({json:{\"image\":item}});\n    };\n  };\n};\n\nreturn array;\n"
      },
      "name": "Array from Object Array1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        760,
        720
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"css\"]}}",
        "allowUnauthorizedCerts": true,
        "responseFormat": "string",
        "options": {
          "fullResponse": false
        },
        "headerParametersUi": {
          "parameter": []
        }
      },
      "name": "HTTP Request5",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        760,
        900
      ]
    }
  ],
  "connections": {
    "HTML Extract1": {
      "main": [
        [
          {
            "node": "IF2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF2": {
      "main": [
        [
          {
            "node": "HTTP Request5",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Array from Object Array1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I found a temporary fix that consists of putting a function node returning items between the HTML Extract node and the If node as such:

{
  "nodes": [
    {
      "parameters": {
        "extractionValues": {
          "values": [
            {
              "key": "image",
              "cssSelector": "img[data-src]",
              "returnValue": "attribute",
              "attribute": "data-src",
              "returnArray": true
            },
            {
              "key": "css",
              "cssSelector": "link[rel=\"stylesheet\"][type=\"text/css\"][href$=\"/css/style.css\"]",
              "returnValue": "attribute",
              "attribute": "href"
            }
          ]
        },
        "options": {}
      },
      "name": "HTML Extract1",
      "type": "n8n-nodes-base.htmlExtract",
      "typeVersion": 1,
      "position": [
        380,
        760
      ]
    },
    {
      "parameters": {
        "functionCode": "return items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        560,
        760
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"css\"]}}",
              "operation": "contains",
              "value2": ".css"
            }
          ],
          "boolean": [
            {
              "value1": "={{$json[\"image\"].length === 0 }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF2",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        720,
        760
      ]
    },
    {
      "parameters": {
        "functionCode": "var array =[];\n\nfor (var item_arr of items) {\n  if (item_arr.json.image) {\n    for (var item of item_arr.json.image) {\n      array.push({json:{\"image\":item}});\n    };\n  };\n};\n\nreturn array;\n"
      },
      "name": "Array from Object Array1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        940,
        640
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"css\"]}}",
        "allowUnauthorizedCerts": true,
        "responseFormat": "string",
        "options": {
          "fullResponse": true
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "sec-ch-ua",
              "value": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
            },
            {
              "name": "accept",
              "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
            },
            {
              "name": "accept-encoding",
              "value": "gzip, deflate, br"
            },
            {
              "name": "accept-language",
              "value": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
            }
          ]
        }
      },
      "name": "HTTP Request5",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        920,
        880
      ]
    }
  ],
  "connections": {
    "HTML Extract1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF2": {
      "main": [
        [
          {
            "node": "HTTP Request5",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Array from Object Array1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Information on the n8n setup

  • n8n version: 0.152.0
  • Database: Postgresql
  • Running n8n via [Docker]: hosted on a server

Hi @TheFSilver, I’m sorry to hear you’re running into this behaviour. I’ve copied the example you have provided, but it doesn’t include any data suggesting the IF node might not work as expected.

Would you be able to share a workflow including mock data using which the behaviour can be seen?

Thanks for your quick answer @MutedJam !

Let’s try with this:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        40,
        320
      ]
    },
    {
      "parameters": {
        "url": "https://www.monika-coiffure.fr/",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        260,
        320
      ]
    },
    {
      "parameters": {
        "extractionValues": {
          "values": [
            {
              "key": "image",
              "cssSelector": "img[data-src]",
              "returnValue": "attribute",
              "attribute": "data-src",
              "returnArray": true
            },
            {
              "key": "css",
              "cssSelector": "link[rel=\"stylesheet\"][type=\"text/css\"][href$=\"/css/style.css\"]",
              "returnValue": "attribute",
              "attribute": "href"
            }
          ]
        },
        "options": {}
      },
      "name": "HTML Extract1",
      "type": "n8n-nodes-base.htmlExtract",
      "typeVersion": 1,
      "position": [
        440,
        320
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"css\"]}}",
              "operation": "contains",
              "value2": ".css"
            }
          ],
          "boolean": [
            {
              "value1": "={{$json[\"image\"].length === 0 }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"css\"]}}",
        "allowUnauthorizedCerts": true,
        "responseFormat": "string",
        "options": {
          "fullResponse": true
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "sec-ch-ua",
              "value": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
            },
            {
              "name": "accept",
              "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
            },
            {
              "name": "accept-encoding",
              "value": "gzip, deflate, br"
            },
            {
              "name": "accept-language",
              "value": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        920,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "var array =[];\n\nfor (var item_arr of items) {\n  if (item_arr.json.image) {\n    for (var item of item_arr.json.image) {\n      array.push({json:{\"image\":item}});\n    };\n  };\n};\n\nreturn array;\n"
      },
      "name": "Array from Object Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        920,
        380
      ]
    },
    {
      "parameters": {},
      "name": "Merge2",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\nitems[0].json = {\n  \"image\": [\n    \"https://www.fakeurl.com/image3.jpg\",\n    \"https://www.fakeurl.com/image4.jpg\"\n  ]\n};\n\n// You can write logs to the browser console\nconsole.log('Done!');\n\nreturn items;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1100,
        540
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "HTML Extract1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTML Extract1": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Array from Object Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Array from Object Array": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}

The culprit might not be the IF node but the Merge node as the issue disappears here:

{
  "nodes": [
    {
      "parameters": {
        "extractionValues": {
          "values": [
            {
              "key": "image",
              "cssSelector": "img[data-src]",
              "returnValue": "attribute",
              "attribute": "data-src",
              "returnArray": true
            },
            {
              "key": "css",
              "cssSelector": "link[rel=\"stylesheet\"][type=\"text/css\"][href$=\"/css/style.css\"]",
              "returnValue": "attribute",
              "attribute": "href"
            }
          ]
        },
        "options": {}
      },
      "name": "HTML Extract1",
      "type": "n8n-nodes-base.htmlExtract",
      "typeVersion": 1,
      "position": [
        440,
        320
      ]
    },
    {
      "parameters": {
        "url": "https://www.monika-coiffure.fr/",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        260,
        320
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"css\"]}}",
              "operation": "contains",
              "value2": ".css"
            }
          ],
          "boolean": [
            {
              "value1": "={{$json[\"image\"].length === 0 }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"css\"]}}",
        "allowUnauthorizedCerts": true,
        "responseFormat": "string",
        "options": {
          "fullResponse": true
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "sec-ch-ua",
              "value": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
            },
            {
              "name": "accept",
              "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
            },
            {
              "name": "accept-encoding",
              "value": "gzip, deflate, br"
            },
            {
              "name": "accept-language",
              "value": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        920,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "var array =[];\n\nfor (var item_arr of items) {\n  if (item_arr.json.image) {\n    for (var item of item_arr.json.image) {\n      array.push({json:{\"image\":item}});\n    };\n  };\n};\n\nreturn array;\n"
      },
      "name": "Array from Object Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        920,
        380
      ]
    },
    {
      "parameters": {
        "functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\nitems[0].json = {\n  \"image\": [\n    \"https://www.fakeurl.com/image3.jpg\",\n    \"https://www.fakeurl.com/image4.jpg\"\n  ]\n};\n\n// You can write logs to the browser console\nconsole.log('Done!');\n\nreturn items;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1100,
        540
      ]
    },
    {
      "parameters": {},
      "name": "Merge2",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1240,
        300
      ]
    }
  ],
  "connections": {
    "HTML Extract1": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "HTML Extract1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Array from Object Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Array from Object Array": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Merge2",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}

Hi @TheFSilver, many thanks for sharing this example!

You’re quite right here, it’d be the Merge Node causing this behaviour and this is the expected behaviour I am afraid. The Merge node is implemented in a way where when it receives data on one input it’ll go up the chain and also execute the nodes connected to the other input.

Is there a way to disable this behaviour on a needed basis from the Merge node?

Hi @TheFSilver, sorry for the late reply - I am afraid that’s not currently possible as confirmed here (you might want to vote on this feature request to make your voice count when it comes to changing this though):

However, as suggested in this feature request, would you need the merge node in your case? In many cases you can consider directly connecting to the node afterwards.

1 Like

Thanks @MutedJam !
I voted and yes, I need that merge.

I actually got several other merge nodes right after that one :rofl: