Need a solution for a loop workflow

Hi,
I need a solution for my workflow:
Let’s say I have a loop using split in batches. In this loop a node is responsible for getting data using api. But this data needs to be filtered.
After the loop is finished there is an email node that sends an email containing those filtered data.

With older versions of n8n I did it this way: I defined a variable in a function node added before the loop using items[0].json.data = []; And in the loop itself I used another function node to push the filtered data in that variable. And after the loop I could access this json.data variable using $node[“Function”].json[“data”].

But with new versions of n8n this approach doesn’t work.

I wonder how this can be done.
Thanks.

Hey @jellybean,

The old approach should still work, Do you get any errors when you try? Are you able to share the workflow as well?

1 Like

Thanks for the reply.
I cannot share the flow. But I can share the function nodes:

function node before the loop:
items[0].json.data = [];

function node inside the loop:

const data = $node["Function"].json["data"]
const response = $node["Api"].json["output"]["response"];

response.forEach(item => {
  if (...) {
    data.push(item);
  }
});

$node["Function"].json["data"] = data;


return items;

And after the loop I could access the data with this expression:
{{$node["Function"].json["data"]}}

Does the workflow below help? It shows a way to merge multiple executions from a node and it might have what you are missing.

2 Likes

Yes that works.
Thank you very much @Jon

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.