0 Not Equal 0 is True?

This evaluated to true…?

image

{
  "name": "Resource GET",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        210,
        370
      ],
      "disabled": true
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "={{$json[\"currentUrl\"]}}",
        "options": {},
        "headerParametersUi": {
          "parameter": []
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "status",
              "value": "approved"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        530,
        370
      ],
      "credentials": {
        "httpHeaderAuth": "SimpleLegal - A24"
      },
      "disabled": true
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "currentUrl",
              "value": "={{$items()[0].json[\"next\"] || 'https://resourceurl/'}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        370,
        370
      ],
      "disabled": true
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{Object.keys($node[\"HTTP Request\"].json[\"next\"]).length}}",
              "operation": "notEqual"
            }
          ],
          "string": []
        },
        "combineOperation": "any"
      },
      "name": "isNextPage",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        420,
        180
      ],
      "disabled": true
    },
    {
      "parameters": {
        "functionCode": "const allItems = []\nlet counter = 0\n\nwhile (true) {\n  try {\n    $items(\"Fetch Page\", 0, counter).map(i => i.json.objects.map(o => allItems.push(o)))\n  } catch (err) {\n    break\n  }\n  counter++\n}\n\nreturn allItems.map(o => ({json: o}))\n\n"
      },
      "name": "Combine Results",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        690,
        -260
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.cursor || \"\"}}"
            }
          ]
        }
      },
      "name": "Empty Cursor",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        290,
        -290
      ]
    },
    {
      "parameters": {
        "functionCode": "items.map(i => {console.log(i)})\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        690,
        220
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isNextPage": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Empty Cursor": {
      "main": [
        [
          {
            "node": "Combine Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "isNextPage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "1"
}

Sorry, do not understand esp. not the attached workflow.

“not equal” is the opposite of “equal”. So:

  • 0 equal 0 => true
  • 0 not equal 0 => false

Here an example workflow where it does exactly that:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      value: 0,\n    }\n  },\n  {\n    json: {\n      value: 1,\n    }\n  }\n];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"value\"]}}",
              "operation": "notEqual"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I agree it should be false, but both 0 === 0 and 0 !== 0 are both going to the true branch.

Did you run my above example workflow?

And is it possible that in your case you have 0 ==="0"? Because that would for example be false.

Going off of Jan’s point, I noticed that you added a “Number” condition (and not a string condition). So if your expression has a string of “0”, then this would not equal the number 0 since they’re different types.

In javascript, you can convert a String to a number with Number(). Here is what that would look like for a variable in an n8n expression:

I’ve highlighted the original variable so it’s easier to see the Number() function.

@jan could it perhaps be an easy improvement to force apply the type conversion under the hood? For example, since a Number condition was added, we expect a number so could just run any Value 1 or 2 input through a Number() function before evaluating the condition (same thing for string etc). Sounds good in theory but wondering if there are any cases where this will lead to issues?

@maxT yes. Currently I use the number, bigger as compare method to compare two strings.
‘a’ < ‘b’
But you can just add this methods.
I could make a pull request for it, inclusive localCompare method.

But than: You can’t compare different values agains each other. Like ‘a’ > 12. Currently I don’t see any use for it at the moment.
(Number(‘a’) == NaN)