If node with expression comparisons, unexpected behavour?

Describe the issue:

how does this pass in true
Using a string condition:


^^ correct

Top condition passes when it shouldnt

Now I know i’m using a number in a string condition however when I use a Number comparison I get these results

I’ve tried multiple ways to get this going however the If condition does not work properly with expressions, Spreadsheet file node - how to return a single row - #7 by wgicio

What is the error message (if any)?

Please share the workflow

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

Hey @wgicio

Thanks for asking that question, first please share some sample if that is possible. I think that this is a problem because both values are expression. Let me explain it on example, let’s say that you have two items:
1: checkID: 1, postid: 1
2: checkID: 2, postid: 1

The if node will loop on all two items and on first loop will catch all items where checkID (1) contain postid (1) => item 1 catches,
In second loop same thing will catch all items where 2 contain 1 => no item catches but if would be catches would be added to results

Not sure if that the error but if you can pass use case and samples I’ll be happy to help you :slight_smile:

1 Like
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        0,
        280
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{Number($node[\"tags-new2\"].json[\"checkID\"])}}",
              "operation": "equal",
              "value2": "={{Number($node[\"Function\"].json[\"idToCheck\"])}}"
            }
          ]
        }
      },
      "name": "IF-matchedID",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        240
      ]
    },
    {
      "parameters": {
        "functionCode": "var strSubject = $node[\"Merge\"].json['subject'];\nvar existingTags = $node[\"Merge\"].json['tags'];\n// in format \n// <site> | <id> | <type> | <date> | <title> | <ownerid> | <phone>\n// RENT | 11668 | active2pending | March 2, 2022 | 5 Bedroom Villa in Ubud (o31f699) | oede5c8 | 6282277436877\n\nvar subVals = strSubject.split(\" | \")\nitem.existingTags = existingTags;\nitem.checkID = Number(subVals[1]);\n\nreturn item;"
      },
      "name": "tags-new2",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        420,
        240
      ]
    },
    {
      "parameters": {
        "url": "https://balicari.com/dummy.json",
        "options": {
          "splitIntoItems": true
        }
      },
      "name": "Merge",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        200,
        280
      ]
    },
    {
      "parameters": {
        "functionCode": "var strSubject = $node[\"grabTestItem\"].json['subject'];\n\n\nvar subVals = strSubject.split(\" | \")\nitems[0].json.idToCheck = Number(subVals[1]);\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        660,
        440
      ]
    },
    {
      "parameters": {
        "operation": "limit"
      },
      "name": "grabTestItem",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        420,
        440
      ]
    },
    {
      "parameters": {
        "content": "So i would expect a true outcome for a match"
      },
      "name": "Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        900,
        200
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "tags-new2": {
      "main": [
        [
          {
            "node": "IF-matchedID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "tags-new2",
            "type": "main",
            "index": 0
          },
          {
            "node": "grabTestItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "grabTestItem": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}```

Have a look at the above test example please.
Basically in the screenshot below the comparison is equal however no True branch is triggered

I’m having unexpected behavour too, for exemple. When I receive whatsapp api call, I check on my Microsoft SQL if that number is on my database, when result is empty all nodes that references SQL output get error and my if statement give me both true and false values, making my flow runs in both ways.

Hey @wgicio, in your example workflow, the Function node would run before the IF node. So while the expression result preview from your screenshot shows two identical numbers, the second value isn’t available during the workflow execution.

You can use a Merge node in Pass-through mode to to make sure both branches have executed and your IF node has access to both. You might also want to add $item(0) to your expression referencing the Function node to make sure the match also works for items other than the first one coming from the tags-new2 branch.

Here’s an example workflow:

Result:

That said, since you are essentially matching two branches you might as well get rid of the IF node and use the Merge node (in Keep Key Matches mode) on its own like so:

This would also return the item you’re looking for:

Hope this helps!

1 Like

Thanks I will have a go over the weekend, the example i gave was just a made up one to have two expressions in the example, my real world data is a bit more complicated and hard to share.
Will keep you posted.
thanks again!

Later I’ll POST using sql exemple.

In my real life example below i see both numbers on the input and output, it it is the case that neither of these values are available for the comparison then the UI is a bit confusing.


In saying that I did some rearranging and used the Key Matches mode and got it working:)

thanks for your help!

1 Like

Glad to hear, thanks so much for confirming @wgicio!

@Fabio_Valentim, unless your issue is identical could you perhaps create a separate thread for your SQL Server issue? Otherwise it might get lost in this one. Thanks so much!

1 Like

Hi @MutedJam , I did few days ago, check in SQL server node result return

Thx, will do!