Asked 1 month ago by JovianRover864
PDF Filtering in n8n Custom Code Node Causes Binary Data Loss
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by JovianRover864
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm building an n8n workflow to process emails by extracting invoice attachments and sending the data to Airtable. Some emails include multiple attachment types (e.g., PDF, PNG, GIF), so I added a custom code node to filter out only PDF files. However, after this node runs, the binary data is no longer recognized, resulting in the following error in the subsequent node:
BASHThe 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
Below is the relevant part of my workflow configuration and custom code used for PDF filtering:
JSON"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 arises because the subsequent node expects a binary file named “data”, but after processing in the custom code node, this binary data isn’t recognized. I need guidance on how to ensure that the filtered PDF binary data is maintained for use in the next node.
Any help would be greatly appreciated!
Comments 0
•Answers 0
•Views 22
No comments yet.