From d1e3c192ba9e2dfd852e570e88f6135d42d2ed45 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 14 Mar 2023 17:47:37 +0000 Subject: [PATCH] fix(Metabase Node): Fix issue with question results not correctly being returned (#5665) --- .../nodes/Metabase/QuestionsDescription.ts | 65 ++++++++----------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts b/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts index 961aa66717..697a230982 100644 --- a/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts +++ b/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts @@ -1,10 +1,4 @@ -import type { - IDataObject, - IExecuteSingleFunctions, - IN8nHttpFullResponse, - INodeExecutionData, - INodeProperties, -} from 'n8n-workflow'; +import type { IDataObject, INodeProperties } from 'n8n-workflow'; import { jsonParse } from 'n8n-workflow'; export const questionsOperations: INodeProperties[] = [ @@ -56,43 +50,38 @@ export const questionsOperations: INodeProperties[] = [ }, output: { postReceive: [ - // @ts-ignore - async function ( - this: IExecuteSingleFunctions, - _items: INodeExecutionData[], - response: IN8nHttpFullResponse, - ): Promise { - const items = _items; - const result: INodeExecutionData[] = []; - for (let i = 0; i < items.length; i++) { - const newItem: INodeExecutionData = { - json: items[i].json, - binary: {}, - }; + async function (this, items, responseData) { + const datatype = this.getNodeParameter('format') as string; - if (items[i].binary !== undefined && newItem.binary) { - Object.assign(newItem.binary, items[i].binary); - } - items[i] = newItem; - if (this.getNode().parameters.format === 'json') { - items[i].json = jsonParse(items[i].json as unknown as string)[0]; - console.log(items[i].json); - delete items[i].binary; - } else { - items[i].binary!.data = await this.helpers.prepareBinaryData( - response.body as Buffer, - 'data', - response.headers['content-type'] as string, - ); - } - result.push(items[i]); + if (datatype !== 'json') { + const binaryData = await this.helpers.prepareBinaryData( + responseData.body as Buffer, + 'data', + responseData.headers['content-type'] as string, + ); + + // Transform items + items = items.map((item) => { + item.json = {}; + item.binary = { ['data']: binaryData }; + return item; + }); + } else { + const results = jsonParse(responseData.body as unknown as string); + items = results.map((result) => { + return { + json: { + ...result, + }, + }; + }); } - return result; + return items; }, ], }, }, - action: 'Result Data a questions', + action: 'Get the results from a question', }, ], default: 'getAll',