Adding data from previous node to every item of another node

Describe the issue/error/question

First of all, thanks for n8n - it’s an amazing tool and I’m happy to have discovered it recently.

I have a Webhook node that gets data sent to it from our web server (list of event participants info + event info), and I then use a Code node to split that JSON array into items:

// Loop over input and turn into items
let results = [];

for (const item of $('Webhook').all()) {
  const students = item.json.body.students;
for (studentKey of Object.keys(students)) {
  results.push({
    json: students[studentKey]
  });
}
}
return results;

This all works great and the downstream nodes get an item for each participant with all relevant info.

However, in several of the downstream nodes I need to add data from each Item with data from the original Webhook node and when doing this, only the first item gets the data from the Webhook node.

For example, I have a Sheets node that adds a row for every participant/item with their name, email and phone number and is supposed to generate a Google Forms prefilled URL with that same info + the event name from the Webhook node (this last bit is the same for every participant). The first participant gets a correctly generated URL but every other participant/row/item is missing the data from the Webhook node. I’m referring it to directly in the Sheets node: $node["Webhook"].json["body"]["event"]["event_name"]

Can you let me know how I can get the data from a previous node to get referenced for every Item actioned on?

Please share the workflow

My workflow snippet here has a Drive node that copies/creates the Sheet used in the Sheets node

Information on your n8n setup

  • n8n version: 0.213.0
  • Database you’re using (default: SQLite): SQLite
  • Running n8n with the execution process: own
  • Running n8n via Docker

Hi @Josh, welcome to the community. I hope I understood what you’re trying to do correctly. I usually try to work with the Merge node here. Like in the example below:

I’m not sure if this is the most effective way though. So maybe someone has an even better idea :slight_smile:

Hi @Josh

I hope I understood correctly, you simply want to reference the first item’s data from the webhook right? You can do that like this in the expression:

$(“Webhook”).first().json[“body”][“event”][“event_name”]

Hi @Niklas_Hatje, thanks very much. I ended up using a Set and then Merge node and that has worked, but I do find it a bit strange that referencing “static” data from an earlier (or otherwise referenced) node isn’t possible when iterating through Items without adding that data to every Item beforehand with a Merge node. Perhaps if someone has time at some point they can explain why that is?

Hi @BramKn Thanks for the reply - this is essentially what I had been trying but that only grabs the data from the Webhook node for the first iteration of Items (ie it only works for the first Item and not for Items 2+)

@Josh you can iterate over the data from the last nodes, but only as long as you’re sure that you have the same amount of items. Your example from above $node["Webhook"].json["body"]["event"]["event_name"] would work if this was the case.

The reason for this is, that n8n nodes run once per item that they get as input. So if you just add data to the items in every step but don’t add elements this would still work. If you change the elements (adding/removing) however, this would not work anymore. Hope that helps?

You can actually already start using our new syntax $("node-name") so:

$("Webhook").item.json["body"]["event"]["event_name"]

That will use our new pairedItem functionality (supported right now by 98% of the nodes and actions), which will then figure out the correct item by itself and should work as you expected originally.

Thanks @jan, this worked great. Much appreciated!

There are more items coming out of the Code node than in the Webhook node so this must have been the issue. Thanks for the clarification.

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