Binary data not recognized after executing custom code

I am working on a flow that takes my emails, filters them for attachments( invoices) and puts the data into an airtable, sometimes there are more items attached to the email than just the PDF, could be PNG or GIF etc.
I got some custom code in a a node that filters only the PDF files and forwards them.

The problem is now, that after the binary data is going throught the custom code its not recognized as binary data anymore, so i cant use it in the next node.

"nodes": [
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.headers.subject }}",
                    "rightValue": "Rechnung",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Rechnung"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "c04177c7-2506-4398-8aea-41c75dca83ae",
                    "leftValue": "={{ $json.headers.subject }}",
                    "rightValue": "Berichtskopie",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Bericht"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        -180,
        20
      ],
      "id": "88a20e79-ec7b-40c2-88a8-862a41806c41",
      "name": "Switch"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "d65293c6-4666-48a9-a734-92ebb3fb553a",
              "leftValue": "={{ $('Email Trigger (IMAP)').item.binary }}",
              "rightValue": "",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        -400,
        0
      ],
      "id": "1cb1cae3-088a-4d68-8a3f-01f79ba7b114",
      "name": "Filter"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Access the binary data of the current item\nconst binaryData = $binary;\n\n// Check if binary data exists\nif (!binaryData) {\n    throw new Error('No binary data found for the current item.');\n}\n\n// Find the PDF file in the current binary data\nlet pdfFile = null;\n\nfor (const key in binaryData) {\n    if (binaryData[key].mimeType === 'application/pdf') {\n        pdfFile = binaryData[key]; // Select the PDF file\n        break; // Stop searching once a PDF is found\n    }\n}\n\n// If a PDF was found, return it\nif (pdfFile) {\n    return {\n        binary: {\n            data: pdfFile,\n        },\n    };\n} else {\n    // Handle cases where no PDF exists in this item's binary data\n    return {}; // Return an empty object or customize the response as needed\n}"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        40,
        0
      ],
      "id": "1824b3ae-c445-44c6-9886-74848a47cccf",
      "name": "Code"
    }
  ],
  "connections": {
    "Switch": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        []
      ]
    }
  },
  "pinData": {}
}


The error i get from this is:

This operation expects the node’s input data to contain a binary file ‘data’, but none was found [item 1]

Make sure that the previous node outputs a binary file

The code i use to filter for PDFs is

// Access the binary data of the current item
const binaryData = $binary;

// Check if binary data exists
if (!binaryData) {
    throw new Error('No binary data found for the current item.');
}

// Find the PDF file in the current binary data
let pdfFile = null;

for (const key in binaryData) {
    if (binaryData[key].mimeType === 'application/pdf') {
        pdfFile = binaryData[key]; // Select the PDF file
        break; // Stop searching once a PDF is found
    }
}

// If a PDF was found, return it
if (pdfFile) {
    return {
        binary: {
            data: pdfFile,
        },
    };
} else {
    // Handle cases where no PDF exists in this item's binary data
    return {}; // Return an empty object or customize the response as needed
}


Happy for any help :slight_smile:

  • n8n version: 1.75.2
  • Database: default
  • n8n EXECUTIONS_PROCESS setting (default: own, main: default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: MacOS

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

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