Code node will only pass one result instead of all results without error

Describe the issue/error/question

I was able to get the flow working and use the code module to strip all the extra data off of my raw links. Multiple resuts are being passed to it but it is only processing and sending the first result.

I believe this is because inside the code module I have “run once for all items” selected. However if I change it to “run once for each item” I get an error "items is not defined [line 11, for item 0] . I’m pretty sure it has to do with my code but this is my first time with javascript and don’t really know what I’m doing. I appreciate any feedback. Thank you.

What is the error message (if any)?

items is not defined [line 11, for item 0]

Please share the workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: Version 0.216.1
  • Database you’re using (default: SQLite): Default
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

The node expects a totally different response depending on the setting. One time it executes once for every item and so expects a single object/item. The other time it executes just a single time and so expects an array of objects/items.

You can see the difference in code very easily when you create a new Code Node and then toggle between both options. That will then probably also clarify how you have to rewrite your code to work in the “run once for each item” option.

Generally, do you want to use $input.item.json, which will contain the item’s data for every iteration.

Hi Jan, thank you for your help! This is the code that I discovered/pieced together and it is working great for me. Hopefully this can help someone else!

// Define the removeQueryString function
function removeQueryString(url) {
  var index = url.indexOf("?");
  if (index !== -1) {
    return url.substring(0, index);
  }
  return url;
}

// Get the original URL from the input data
var originalUrl = $json.data.hdplay;

// Call the removeQueryString function to strip the URL
var strippedUrl = removeQueryString(originalUrl);

// Output the stripped URL as an output of this node
return { json: { strippedUrl: strippedUrl } };
1 Like

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