Code so confusing

I dont understand the code node until now. Why the developer makes it so complicated? For example there is no built in method to get exact value sent by previous node. Instead just simple variable “$input”, We need to use “$input.all()[0].json” just to get that value. I guess theres many people strugle to grasp how “code node” works too.

And why there should be “Run Once for All Items” and “Run Once for Each Item” If we can use the javascript looping to do all of this in this node. Thats just make it so confusing.

Because the Code node means ‘Code’. It’s for advanced users who are familiar with programming.

But generally, it’s not the node feature, it’s how the n8n handle the references. All methods are described in the docs

Can you explain what is the purpose of “Run Once for All Items” and “Run Once for Each Item” ? If everything can be done using vanilla javascript loop inside this node?

It’s a little bit of a learning curve, but insanely powerful capability.
I had to update 260 URL’s in seven languages in a Baserow DB

  1. Pulled the 260 rows with 7 tables from Baserow
  2. Processed all rows in a code node, one by one “Run once for all items”
  3. Updated the DB with new URL’s

The URL re-writing in the code node took no observable time, I though it had skipped at first.

Here’s code for each language, I don’t think object-references are that complex…

item.json.ES - the Spanish URL in current DB row

I’m sure this can be optimized, I’m not that good with JS so I do it step-by-step…

 if (item.json.ES !== null ) {                //check if URL exists    
     urlParts = item.json.ES.split('/');      // split up on slash 
     urlParts.splice(3, 0, 'all');            // insert 'all' after host 
     const modifiedES = urlParts.join('/');    // re-assemble
     item.json.ES = modifiedES;                // update the object
 }

The Run Once for All Items means if there are 5 items before the Code node, then it will be executed only once and contain the info about every 5 items ($input.all() method )

The Run Once for Each Item means if there are 5 items before the Code node, then it will be executed 5 times. One time per every item. And it will contain only that item ($input.item property)

1 Like