How to return binary data in custom node

Describe the problem/error/question

Can you please help me with my code for returning binary data?

My custom node binary data only outputs 1 item even if it receives multiple inputs. I need help to correctly populate the binary property for the output, with all the files showing correctly.

What is the error message (if any)?

No error, I want to correctly return the binary property files for my custom node. Currently it only creates 1 binary item which is the 1st input received. It returns correctly the json items but the binary property only contains 1 file (the 1st one processed).

Please share your workflow

// generateReportFromReportDefinition
if (resource === 'report') {
	if (operation === 'generateReportFromReportDefinition') {



		let endpoint = `https://portal.mobilefieldreport.com/mfr/Report`

		const ServiceRequestUI = this.getNodeParameter('ServiceRequest', i) as IDataObject;
		const ServiceRequestId = ServiceRequestUI.value
		const ReportDefinitionCode = this.getNodeParameter('ReportDefinitionCode', i) as string;

		body.ServiceRequestId = ServiceRequestId
		body.ReportDefinitionCode = ReportDefinitionCode
		body.IsInvoice = false


		const optionsFirstRequest = {
			method: 'POST',
			body: body,
			qs,
			uri: endpoint,
			json: true,
			useQuerystring: true,
		} satisfies IRequestOptions;

		console.log(optionsFirstRequest)

	let responseDataFirstRequest = await this.helpers.requestWithAuthentication.call(
			this,
			'mfrApi',
			optionsFirstRequest,
	);

	let ReportDtoUri = responseDataFirstRequest.ReportDto.URI
	let DocumentName = responseDataFirstRequest.ReportDto.DocumentName

	const optionsSecondRequest = {
			method: 'GET',
			uri: ReportDtoUri,
			json: false,
			encoding: null
		} satisfies IRequestOptions;


		const pdfBuffer = await this.helpers.requestWithAuthentication.call(
		this,
		'mfrApi',
		optionsSecondRequest,
	);


			const newItem: INodeExecutionData = {
						json: items[i].json,
						binary: {},
						pairedItem: { item: i },
					};

					if (items[i].binary !== undefined) {
						// Create a shallow copy of the binary data so that the old
						// data references which do not get changed still stay behind
						// but the incoming data does not get changed.
						Object.assign(newItem.binary!, items[i].binary);
					}

					items[i] = newItem;

					items[i].binary!['file'] = await this.helpers.prepareBinaryData.call(
       		 this,
       		 pdfBuffer, // raw Buffer
       		 DocumentName,  // original filename
      	)


}}



// end

if(resource === 'report' && operation === 'generateReportFromReportDefinition'){
		// For file downloads the files get attached to the existing items
			return [items];
}

else{
	const executionData = this.helpers.constructExecutionMetaData(
		this.helpers.returnJsonArray(responseData as IDataObject[]),
		{ itemData: { item: i } },
	);
	returnData.push(...executionData);
}

Share the output returned by the last node

The binary data only containing 1 file:

The json data having the correct number of items and link:

I want the binary data to be returned like this (supports multiple files):

Appreciate your help in advance!

Thank you,
Alex

Information on your n8n setup

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

The issue is this part:

if(resource === ‘report’ && operation === ‘generateReportFromReportDefinition’){
// For file downloads the files get attached to the existing items
return [items];
}

it returns just 1 item. How can I adapt it?