Return multiple Items from Code Node

Hi everyone,

how can i return multiple Items from the Code Node? this is my first time using it since it replaced the function node and i cant get it to work.

I want to make several http-requests off an array from a single incoming Item.

No matter what i try, i am always forced to return a single item or it errors out.
The relevant data is structured like this, i copied it from the node input json view:

[
  {
    "body": {
      "data": [359, 121, 223, 343]

    }
  }
]

Can you guide me in the right direction here? Any help is much appreciated!

Can you provide an example workflow?

Maybe try adding items to the returned array itself

[
  {
    "body": {
      "data": 359

    }
  },
 {
    "body": {
      "data": 121

    }
  }
...
]

For example returning

return [
{
"name":
"First item",
"code":
1
},
{
"name":
"Second item",
"code":
2
}
]

gives me two items as output

1 Like

Thanks! I now got it to work, yet i don’t completely understand why.
first, the tl;dr for others having this issue is:

let data = $input.first().json.data

return data.map((value)=> {
  return {json:{value}}
})

when there is only one incoming item to this node per workflow this does the trick. But this is also what confuses me.
I had the node running in “Run once for each Item” mode because it sounded to me like the appropriate mode for this. "Run once for all Items " sounds like being mainly meant to be used for aggregating Information from multiple incoming nodes.
I still cannot return multiple items in “Run once for each Item” mode, even when hardcoding the response. is there a way to do this?

When using the Run once for each Item mode each incoming item must correspond to a result item and your code would only access a single item at a time.

So, while you wouldn’t have to worry about looping you also can’t change the total number of items in this mode. Or in other words, something like one incoming item to three result items won’t work in this mode. Does this make sense?

1 Like