Function in a Loop

Hi, i have made this. But i have the problem that my Service runs only once with first entry from maybe 5 entrys of a mysql query. Till the data set i have 2 entrys. What i can do ?

Here are the function:

return [
  {
  json: {
    
  },
  binary: {
    data: {
      data: $node["Set"].json["content"],
      mimeType: $node["Set"].json["mime-type"],
      fileExtension: 'pdf',
      fileName: $node["Set"].json["file-name"]
    }
  }
  }
]

Thanks,
Stefan

Please check the documentation of the Function-Node and of the methods you are using:
http://docs.n8n.io/#/nodes?id=function-node

But in a nutshell. You return an array with one item and that is exactly what n8n does process. The one item you return. If you want that it processes multiple items you have to return multiple ones.

So what you want to do is to iterate over all incoming items which can be found in the variable items (as described in the above documentation). You can then either add the binary property with the appropriate data to the existing items and then return that one or create a new array and then return that one.

Hope that makes sense and helps. But I am sure if you check the documentation you will understand what I am talking about.

Jan, i am a newbie in this field. Do you have a example ?

You mean this example ?

// Sets the JSON data property "myFileName" of the first item to the name of the
// file which is set in the binary property "image" of the same item.
items[0].json.myFileName = items[0].binary.image.fileName;

return items;

First solution that i have are:

items[0].json.allItems = items[0][
  {
  json: {
    
  },
  binary: {
    data: {
      data: $node["Set"].json["content"],
      mimeType: $node["Set"].json["mime-type"],
      fileExtension: 'pdf',
      fileName: $node["Set"].json["file-name"]
    }
  }
  }
]
return items;

But the output is not more binary !

No, it would have to be something like that:

const returnItems = [];

for (const item of items) {
  returnItems.push({
    json: {},
    binary: {
      data: {
        data: item.json["content"],
        mimeType: item.json["mime-type"],
        fileExtension: 'pdf',
        fileName: item.json["file-name"]
      }
    }
  });
}

return returnItems;