Switch node to more than one path

I have configured a switch node in a workflow with regex. If a value is met twice or three times, it should go all the necessary paths.
Unfortunately in my example it is runnig only to path0 for the example value of “Tarif” = “BP700”.

For this example value it should go through outputKey0 and outputKey1. How can I achieve that?

Thank you!

switch node config

    {
      "parameters": {
        "dataType": "string",
        "value1": "={{ $('Merge').item.json.rawRequest.Tarif }}",
        "rules": {
          "rules": [
            {
              "operation": "regex",
              "value2": "BP(300|700|900|1100)",
              "outputKey": "0"
            },
            {
              "operation": "regex",
              "value2": "BP(400|700|900|1100)",
              "outputKey": "1"
            },
            {
              "value2": "BP250",
              "outputKey": "2"
            },
            {
              "operation": "regex",
              "value2": "BP(100|200|900|1100)",
              "outputKey": "3"
            }
          ]
        }
      },
      "id": "21281a8a-71ee-4340-90d5-5bd1bc6bd95b",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 2,
      "position": [
        1020,
        1060
      ]
    }

Information on your n8n setup

  • n8n version: 1.15.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Ubuntu 22.04

Hey @mokkin,

It seems like the switch node acts as if you would add a break; statement after each case in js, and thus only ever returns an output at the first match of your regex. You can see the same behavior with this simple test setup:

I think this is probably caused by line 688 here: https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/Switch/V2/SwitchV2.node.ts

It would be cool if this option would be configurable and you could choose if the switch node is able to assign an item to multiple outputs.

I think your best option at the moment, is just to use 4 "IF-Nodes" (if you only have the four options you showed in your switch node config).

This way you would evaluate every option individually and bypass this problem.

I hope that helps :slight_smile:

3 Likes

Hi folks, @one_juru is spot on here.

Instead of the IF node you could also consider using Filter nodes. They are almost identical to the IF node but have only one output, so will keep the workflow just a little bit cleaner.