How to reference the execution of a node from a previous loop?

Describe the problem/error/question

Hello ^^

I am working today on a workflow to create some google document and then fill them up with product titles and information.

The trick is, I need to create only one document per shop and then fill them with every product coming from this shop.

Example : I have 3 products

  • Product 1 comes from Shop A
  • Product 2 comes from Shop A
  • Product 3 comes from Shop B
    => I must have at the end
  • Document A with Product 1 and 2 info
  • Document B with Product 3 info

So, my reasoning was the following. I can order my products by shops with the items list node. Then, I can create a loop that will :

  • On the first iteration, create a document and add the info of the first product
  • On the second and following iteration, check if the product comes from the same shop than the last iteration product
  • If yes, I can add the information to the current document
  • If no, I can create a new document for this shop.

What is the error message (if any)?

Only problem is, when I get to the node that check if the current shop is the same than the previous iteration, I get the error : ERROR: invalid syntax

So from my understanding, I am then not able to reference the info of a previous node ?

Please share your workflow

Thanks for your advices and if you have any workaround, that would help

Information on your n8n setup

  • n8n version: cloud
  • Running n8n via (n8n cloud):
  • Operating system: Windows

Hi @Romain_Mangattale, I don’t know your exact data structure, but I reckon you have three n8n items here (one for each product across all possible shops)? I think in such a scenario it could make sense to break down your data into one item per shop (and document) in the first step.

Next create the documents for each shop, before finally looping through all products for each shop. Here’s how this could look like in a workflow:

This will leave you with the two documents you have in mind, without any loops:

The one thing you want to be careful about is the expression on the Set node “Fetch products for each shop” in this example:

{{
  $('Mock data')
    .all()
    .map(e => e.json)
    .filter(e => e.shop_name === $json.shop_name)
}}

This fetches all items for the current shop from my node called Mock data by comparing fields called shop_name. Chances are you use different node and field names. So you might need to replace the respective names in this example.

Hope this helps!

1 Like

Hello !

Yes, that solution works ^^ Had to customize a bit to match my data but it responds to my needs :slight_smile:

Thanks again !

1 Like

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