Merge many inputs in one output

Not sure API supports something like this but it seems very popular case when we have many inputs, process them with Function and return one stream as result.
Could someone point me out on possible solution of this problem?
Thanks.

1 Like

@mrjazz welcome to the community.

Did you already check the merge node? It seems to me it will solve the issue you are describing.

Merge does the trick for two inputs, what if I have more inputs or from every input receive many job executions (for me they are something like threads)

Honestly do not understand what exactly you want to do but there is currently no node that has more than 2 inputs.

You can however collect the data manually from as many nodes as you want in a Function-Node and the $items() method:
https://docs.n8n.io/nodes/expressions.html#method-items-nodename-string-outputindex-number-runindex-number

Thanks for your efforts to help me.
What I mean something like that: http://i.imgur.com/RhPa3QS.png

My function dumps $items() three times. What I mean, is some ability to process all the inputs in Function and then sent to Function1 just as one output (and process Function1 once, not 3 times as on the screen).
Hopefully it has some sense now.

Yes, that does not work. Every time you connect multiple nodes to one node input it will execute once for each of them. In this case, you would have to use two Merge-Nodes.

If you, however, connect only one node to the IF-Node it will just execute once and you can still query the data of all the other nodes.

Is there any way process just one execution and ignore others (I think I can find workaround)?
I mean in terms of my example above, process all jobs in Function and process just first one in Finction1.
I am trying to do that with IF( $runIndex == 0) but it doesn’t work for me.

You can do that by just connecting one of the “Execute Command” nodes. Even though the others are not connected you can still query their data with $items().

I mean the following. In my example:

Finction process all three jobs, Function1, after all jobs processed by Function, process just one of them or call once. Is it possible?

Ah yes, I know. You would simply disconnect two of the “Execute Command” nodes. It will then work fine.

Because it starts from Start-Node and adds its connections (the tree Execute Command Nodes) to the Execution-Stack. After each of them got executed it would add its connections to the stack (only one of the three would be connected to another one, does not matter which). As its connections would be in the stack after the Execute Command nodes, would the Function-Nodes always be executed after the Execute Command Nodes.

If I’ll do it like that then Function won’t process results from each of them (that is necessary). Plus there will be no guarantee Function will be called after Command 1…3 finish.

Did you read the documentation for $items() which I linked above? There it describes how you can access the data of a specific node. If you follow that you can get the data of all 3 nodes.

I actually already explained above exactly why it is guaranteed that the Function-Node will execute after Command 1 …3 did finish. That is sadly all I can say about that. You can literally believe me or not.

If you do not believe me, use Merge-Nodes as @RicardoE105 said in his initial answer. You would have to use two of thoes. One to Merge the data of 1 & 2 and then a second to also merge the data of 3 in.

1 Like

Thanks, Jan, I see your point. Yes, you are right about guarantee. Can you describe me a little $items() logic. When I call $items(‘Execute Command’) I get input from it but when I call $items() I get current execution. Is there any way to iterate through all inputs?

The Function-Node has only one input. So iterating is not needed or possible. There is no way to iterate over all of the nodes that are connected to that one input. That is simply not how n8n works. Data flows from one node to another and each input receives data from one node.

Like I wrote will the node execute once for every node that is connected to that one input it has. For that reason do you have to make sure to only have one of the three nodes is connected and disconnect the other two. Then you can have three different $items calls with the name of that nodes.

I see, thanks Jan. If I would like to send an email after processing by Function what is the right way to do that only once?

That depends then fully on how you return the data. If you combine all of that incoming data into one item the following Send EMail node would only receive one item and would so send only one email.

The most basic code for a Function-Node doing that would look like this:

return [
  {
    json: {
      message: "The email text"
    }
  }
];

Jan, thanks for your help and patience. I think I found solution with IF node and condition {{$runIndex==0}}. It pass just one execution job through it. Exactly what I was looking for.

If that is exactly what you wanted, then I totally misunderstood what you wanted to do. Always thought you were interested in the data of all the nodes. But if you just wanted the data of the first one that executes then you can block the other two like this. Happy that you found a solution.

Yes, I felt some misunderstanding, thanks for the help