If block matches string literal, but not string expression

Hi again! I’m still quite new here, but I was making good progress until I hit an issue with the If block that is driving me bonkers.

I’m trying to use an If block to match against a string evaluated from an expression. I can match data against a string literal without issue, but if I use an expression that produces the exact same string, it doesn’t match. What could I possibly be doing wrong here?

Thanks so much for your patience.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      name: 'bar'\n    }\n  },\n  {\n    json: {\n      name: 'foo'\n    }\n  }\n];"
      },
      "name": "Test Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      name: 'foo'\n    }\n  }\n];"
      },
      "name": "Search Value",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "foo"
            }
          ]
        }
      },
      "name": "Literal Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        400
      ],
      "notesInFlow": true,
      "notes": "Works"
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "={{$node[\"Search Value\"].json[\"name\"]}}"
            }
          ]
        }
      },
      "name": "Expression Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        200
      ],
      "notesInFlow": true,
      "notes": "Doesn't work"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Search Value",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test Data": {
      "main": [
        [
          {
            "node": "Literal Match",
            "type": "main",
            "index": 0
          },
          {
            "node": "Expression Match",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Value": {
      "main": [
        [
          {
            "node": "Test Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

As we do not like people to be driven bonkers I try my best to help you!

You are having this issue because you are comparing the data of two nodes that have a different number of items.

Search Value:

  1. Item: { name: ‘foo’ '}

Test Data:

  1. Item: { name: ‘bar’ '}
  2. Item: { name: ‘foo’ '}

So in the IF-Node the following is happening because n8n automatically compares the items with the same index:

  1. Item: It compares “foo” with “bar” => does not match so false
  2. Item: It compares undefined with “foo” => does not match so false

Hope that helps!

Thank you @jan! This makes sense. And if someone stumbles on this thread with the same problem/question, here is what I needed to use as an expression:

{{$item(0).$node["Search Value"].json["name"]}}

This pins the value received from the “Search Value” node at item 0 so it is always used as a comparator. Here’s the fixed workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      name: 'bar'\n    }\n  },\n  {\n    json: {\n      name: 'foo'\n    }\n  }\n];"
      },
      "name": "Test Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      name: 'foo'\n    }\n  }\n];"
      },
      "name": "Search Value",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "foo"
            }
          ]
        }
      },
      "name": "Literal Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        400
      ],
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "value2": "={{$item(0).$node[\"Search Value\"].json[\"name\"]}}"
            }
          ]
        }
      },
      "name": "Expression Match",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        200
      ],
      "notesInFlow": true
    }
  ],
  "connections": {
    "Test Data": {
      "main": [
        [
          {
            "node": "Literal Match",
            "type": "main",
            "index": 0
          },
          {
            "node": "Expression Match",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Value": {
      "main": [
        [
          {
            "node": "Test Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like