Skip to content

Merge Node: Upstream node reference $('NodeName').item fails for non-first inputs #15981

Description

@ldaniel-jmz

Bug Description

When using a Merge node (in "append" mode, for example), attempting to reference an upstream node's data using the $('NodeName').item expression fails for items that originated from the Merge node's second input (or subsequent inputs). The expression works correctly for items that originated from the Merge node's first input.
This behavior is a regression, as it was working correctly in n8n version 1.82.2.
The error typically shown is "ERROR: Can't determine which item to use" or a similar paired item resolution failure.

To Reproduce

To Reproduce:

  1. Create a new workflow with the following nodes:
    • "Start" node.
    • Three "Set" nodes (e.g., Set variable_1, Set variable_2, Set variable_3).
      • Set variable_1 sets a field, e.g., variable_1 to 1234.
      • Set variable_2 sets a field, e.g., variable_2 to 2345.
      • Set variable_3 sets a field, e.g., variable_3 to 5678.
    • A "Merge" node.
      • Connect Set variable_1 to Input 1 of the Merge node.
      • Connect Set variable_2 to Input 2 of the Merge node.
      • Connect Set variable_3 to Input 3 of the Merge node.
      • Set Merge node "Mode" to "Append".
    • A final "Set" node (let's call it Output_Node) connected after the Merge node.
      • In Output_Node, create a field final_variable_1 with the expression: {{ $('Set variable_1').item.json.variable_1 }}
      • In Output_Node, create a field final_variable_2 with the expression: {{ $('Set variable_2').item.json.variable_2 }}
      • In Output_Node, create a field final_variable_3 with the expression: {{ $('Set variable_3').item.json.variable_3 }}
  2. Execute the workflow.
  3. Observe the output of the Output_Node.
    • For the item that originated from Set variable_1 (Merge Input 1), final_variable_1 will correctly show the value (e.g., 1234).
    • For the item that originated from Set variable_2 (Merge Input 2), final_variable_2 will fail with an error like "ERROR: Can't determine which item to use".
    • A similar failure will occur for final_variable_3.
      (Attached screenshots show the successful retrieval for final_variable_1 and the error "Can't determine which item to use" for final_variable_2 when inspecting the Output_Node results for the item originating from Set variable_2).

Image

Image

Image

Image

(Bug Test Workflow JSON):

{
  "name": "Bug Test Workflow",
  "nodes": [
    {
      "parameters": {
        "numberInputs": 3
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.1,
      "position": [
        460,
        200
      ],
      "id": "92aad872-f96b-4785-a858-cf6ea48ef34c",
      "name": "Merge",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "63830a30-a4cc-4a66-9d01-8f0a058d4d43",
              "name": "variable_1",
              "value": "1234",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        160,
        0
      ],
      "id": "54eaa8ed-b3e8-4c59-af23-9c74d0f15f95",
      "name": "Set variable_1"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "63830a30-a4cc-4a66-9d01-8f0a058d4d43",
              "name": "variable_2",
              "value": "2345",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        160,
        200
      ],
      "id": "d3ecaea8-e30c-4911-9adf-78e12df5a7ac",
      "name": "Set variable_2"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "63830a30-a4cc-4a66-9d01-8f0a058d4d43",
              "name": "variable_3",
              "value": "3456",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        160,
        400
      ],
      "id": "2e65f7a3-d1f8-44b4-9199-671ba502dd2e",
      "name": "Set variable_3"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ed13172d-9bae-4b21-b168-577eee217e24",
              "name": "final_variable_1",
              "value": "={{ $('Set variable_1').item.json.variable_1 }}",
              "type": "string"
            },
            {
              "id": "7a92607d-d2f1-4b38-855f-4ec19b70f414",
              "name": "final_variable_2",
              "value": "={{ $('Set variable_2').item.json.variable_2 }}",
              "type": "string"
            },
            {
              "id": "89862e7d-0c44-4d6a-897e-a249c06f6346",
              "name": "final_variable_3",
              "value": "={{ $('Set variable_3').item.json.variable_3 }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        680,
        200
      ],
      "id": "898d35e4-42a7-4fad-8a01-3f0d6d966cab",
      "name": "Output"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -140,
        200
      ],
      "id": "12501aa5-502f-4960-a44a-833d017a6eb7",
      "name": "Manual Trigger"
    }
  ],
  "pinData": {},
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set variable_1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set variable_2": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Set variable_3": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Set variable_2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a3cd04e1-304b-4752-8989-d299c622495c",
  "meta": {
    "instanceId": "b7c05a8986e66772aa7af4004a000d6c3b16ca93d88d75938718485e254b5a15"
  },
  "id": "QNCbSmpQ2GNK0w3R",
  "tags": []
}

Expected behavior

The expressions {{ $('Set variable_2').item.json.variable_2 }} and {{ $('Set variable_3').item.json.variable_3 }} (and similar references to upstream nodes connected to non-first inputs of the Merge node) should correctly resolve and return the values set in their respective source nodes.
The behavior should be consistent for all items passing through the Merge node, regardless of which input they came from, and match the behavior of n8n v1.82.2.

Operating System

Ubuntu 22.04.5

n8n Version

1.94.1

Node.js Version

18.19.1

Database

PostgreSQL

Execution mode

queue

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions