Editing a custom Function Node code to iterate

I know i’m close, just can’t think what I’m missing.

I have this flow

The “Set” node takes some data and sets a base64 of an image

My function node does this, but only on the first image

    return [
  {
  json: {
    
  },
  binary: {
    data: {
      data: $node["Set1"].json.data,
      mimeType: "image/png",
      fileExtension: 'png',
      fileName: $node["Merge1"].json["message"].toString().replace('/target/','')+".png",
    }
  }
  }
]

and that all works fine as it should except I only get the first result, not the full 19.

I know I need to tell it to read it as an array, but my brain is just dead and won’t let me think.

what am I missing here? I know it’s something obvious.

I’m closer but keep getting the same file over and over again with the same name and output

const IterateImages = [];

for (const item of items) {
  IterateImages.push({
    json: {},
    binary: {
    data: {
      data: $node["Set1"].json.data,
      mimeType: "image/png",
      fileExtension: 'png',
      fileName: $node["Merge1"].json["message"].toString().replace('/target/','')+".png",
      }
    }
  });
}

return IterateImages;

You might want to change $node["Set1"].json.data for item.json.data.

To fix the issue with the second reference, the easiest way would be to use a merge before the function node to make that data available in the input. Then, you can do item.json["message"].toString().replace('/target/','')+".png". You can probably still do it using the $node[], but you will need to change the for loop to expose the item’s index.

ok cool, i’ll have a play. thank you

ok cool so i now get the

and it creates the files
image

but the files are dud, something isnt saving them correctly in the content

the node before is now

Wha is the output of get Grab Victim Logos and where do you want to send this images to? I cannot see the node after Write Binary File1

oh it will get posted to HTTP node, but before that the file needs to save to disk, unless it doesnt have to, but for sure the content of the file isnt what its meant to be.

and then once its written the file to disk i can then post it to my site as part of the rest of the flow

But i have got it to work in this flow but it doesn’t iterate, its just one file in and one out

{

  "name": "Upload Image to Wordpress Workflow TEMPLATE",

  "nodes": [

    {

      "parameters": {},

      "name": "Start",

      "type": "n8n-nodes-base.start",

      "typeVersion": 1,

      "position": [

        240,

        300

      ]

    },

    {

      "parameters": {

        "functionCode": "const company_logo = items[0].json.company_logo\n\nconst regex = /(base64[^\"]+)/gmi;\n\nconst matched = company_logo.match(regex);\n\nreturn [\n  {\n    json: {\n      company_logo: matched.toString().split(',')[1]\n    }\n  }\n]\n"

      },

      "name": "Function",

      "type": "n8n-nodes-base.function",

      "typeVersion": 1,

      "position": [

        630,

        300

      ]

    },

    {

      "parameters": {

        "fileName": "/files/victim-test.png"

      },

      "name": "Write Binary File",

      "type": "n8n-nodes-base.writeBinaryFile",

      "typeVersion": 1,

      "position": [

        1000,

        300

      ]

    },

    {

      "parameters": {

        "functionCode": "return [\n  {\n  json: {\n    \n  },\n  binary: {\n    data: {\n      data: $node['Function'].json.company_logo,\n      mimeType: \"image/png\",\n      fileExtension: 'png',\n      fileName: $node[\"Set\"].json[\"name\"]+\".png\"\n    }\n  }\n  }\n]\n"

      },

      "name": "Function1",

      "type": "n8n-nodes-base.function",

      "typeVersion": 1,

      "position": [

        800,

        300

      ]

    },

    {

      "parameters": {

        "values": {

          "string": [

            {

              "name": "company_logo",

              "value": "<img src=\"data:image/png;base64,REPLACE-ME-WITH-VALID-BASE64\" />"

            },

            {

              "name": "name",

              "value": "234"

            }

          ]

        },

        "options": {}

      },

      "name": "Set",

      "type": "n8n-nodes-base.set",

      "typeVersion": 1,

      "position": [

        430,

        300

      ]

    },

    {

      "parameters": {

        "authentication": "basicAuth",

        "requestMethod": "POST",

        "url": "https://www.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

        "responseFormat": "file",

        "jsonParameters": true,

        "options": {

          "bodyContentType": "multipart-form-data",

          "bodyContentCustomMimeType": "image/png"

        },

        "sendBinaryData": true

      },

      "name": "WordPress Upload Media",

      "type": "n8n-nodes-base.httpRequest",

      "typeVersion": 1,

      "position": [

        1230,

        300

      ],

      "executeOnce": false,

      "credentials": {

        "httpBasicAuth": "xxxxxxxxxxxxxxxxxxxxxxxxx"

      }

    }

  ],

  "connections": {

    "Function": {

      "main": [

        [

          {

            "node": "Function1",

            "type": "main",

            "index": 0

          }

        ]

      ]

    },

    "Function1": {

      "main": [

        [

          {

            "node": "Write Binary File",

            "type": "main",

            "index": 0

          }

        ]

      ]

    },

    "Set": {

      "main": [

        [

          {

            "node": "Function",

            "type": "main",

            "index": 0

          }

        ]

      ]

    },

    "Write Binary File": {

      "main": [

        [

          {

            "node": "WordPress Upload Media",

            "type": "main",

            "index": 0

          }

        ]

      ]

    },

    "Start": {

      "main": [

        [

          {

            "node": "Set",

            "type": "main",

            "index": 0

          }

        ]

      ]

    }

  },

  "active": false,

  "settings": {},

  "id": 32

}

Hey @RedPacketSec!

Did you try using the Function Item node? That node will process all the incoming data, while with the Function node, you will need to create a loop, process the data, and return an array with the items.

i found a solution in a different thread, i will link it here:

This allowed me to iterate over the files.

I have since built out a complex workflow with the amazing help of the forum mods and the custom javascript that we all struggle with.

1 Like