Multiple Items returned as single item

Describe the issue/error/question

I am developing my first node and am experiencing a seemingly common error whereby I would like to make an endpoint return a nested array as a single item. I know there is a code snippet to make this work, however, I keep running into an issue.

return items[0].json.map(item => {
  return {
    json: item
  }
});

This is the data structure.

[
	[
			{
					"username_count": 0,
					"description": "Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges containing Success Audit: A scheduled task was updated ",
					"rules": [
							{
									"id": 100407,
									"type": "CRE_RULE"
							}
					],
					"event_count": 13,
					"flow_count": 0,
					"assigned_to": null,
					"security_category_count": 3,
					"follow_up": false,
					"source_address_ids": [
							6
					],
					"source_count": 1,
					"inactive": false,
					"protected": false,
					"closing_user": null,
					"destination_networks": [
							"Net-10-172-192.Net_172_16_0_0"
					],
					"source_network": "Net-10-172-192.Net_172_16_0_0",
					"category_count": 3,
					"close_time": null,
					"remote_destination_count": 0,
					"start_time": 1639764968556,
					"magnitude": 4,
					"last_updated_time": 1639775131611,
					"credibility": 3,
					"id": 20,
					"categories": [
							"Information",
							"Misc Suspicious Event",
							"Scheduled Task Creation Success"
					],
					"severity": 6,
					"policy_category_count": 0,
					"log_sources": [
							{
									"type_name": "EventCRE",
									"type_id": 18,
									"name": "Custom Rule Engine-8 :: prod-qrdr01",
									"id": 63
							},
							{
									"type_name": "WindowsAuthServer",
									"type_id": 12,
									"name": "WindowsAuthServer @ PROD-WKST01",
									"id": 112
							}
					],
					"closing_reason_id": null,
					"device_count": 2,
					"offense_type": 0,
					"relevance": 2,
					"domain_id": 0,
					"offense_source": "172.16.20.129",
					"local_destination_address_ids": [
							4
					],
					"local_destination_count": 1,
					"status": "OPEN"
			},
			{
					"username_count": 0,
					"description": "Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges containing Success Audit: A scheduled task was updated ",
					"rules": [
							{
									"id": 100407,
									"type": "CRE_RULE"
							}
					],
					"event_count": 28,
					"flow_count": 0,
					"assigned_to": null,
					"security_category_count": 3,
					"follow_up": false,
					"source_address_ids": [
							6
					],
					"source_count": 1,
					"inactive": true,
					"protected": false,
					"closing_user": null,
					"destination_networks": [
							"Net-10-172-192.Net_172_16_0_0"
					],
					"source_network": "Net-10-172-192.Net_172_16_0_0",
					"category_count": 3,
					"close_time": null,
					"remote_destination_count": 0,
					"start_time": 1638555204525,
					"magnitude": 2,
					"last_updated_time": 1638628785460,
					"credibility": 3,
					"id": 19,
					"categories": [
							"Information",
							"Misc Suspicious Event",
							"Scheduled Task Creation Success"
					],
					"severity": 6,
					"policy_category_count": 0,
					"log_sources": [
							{
									"type_name": "EventCRE",
									"type_id": 18,
									"name": "Custom Rule Engine-8 :: prod-qrdr01",
									"id": 63
							},
							{
									"type_name": "WindowsAuthServer",
									"type_id": 12,
									"name": "WindowsAuthServer @ PROD-WKST01",
									"id": 112
							}
					],
					"closing_reason_id": null,
					"device_count": 2,
					"offense_type": 0,
					"relevance": 0,
					"domain_id": 0,
					"offense_source": "172.16.20.129",
					"local_destination_address_ids": [
							4
					],
					"local_destination_count": 1,
					"status": "OPEN"
			}
	]
]

What is the error message (if any)?

I keep getting the following error, I feel like it should be a simple fix but I am not much of a javascript developer.

TypeError: Cannot read property 'map' of undefined

I tried just implementing this as my code.

return returnData[0].json.map(item => {
			return {
				json: item
			}

Any help would be greatly appreciated.

Share the output returned by the last node

This is the desired output.

[
    {
        "username_count": 0,
        "description": "Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges containing Success Audit: A scheduled task was updated ",
        "rules": [
            {
                "id": 100407,
                "type": "CRE_RULE"
            }
        ],
        "event_count": 13,
        "flow_count": 0,
        "assigned_to": null,
        "security_category_count": 3,
        "follow_up": false,
        "source_address_ids": [
            6
        ],
        "source_count": 1,
        "inactive": false,
        "protected": false,
        "closing_user": null,
        "destination_networks": [
            "Net-10-172-192.Net_172_16_0_0"
        ],
        "source_network": "Net-10-172-192.Net_172_16_0_0",
        "category_count": 3,
        "close_time": null,
        "remote_destination_count": 0,
        "start_time": 1639764968556,
        "magnitude": 4,
        "last_updated_time": 1639775131611,
        "credibility": 3,
        "id": 20,
        "categories": [
            "Information",
            "Misc Suspicious Event",
            "Scheduled Task Creation Success"
        ],
        "severity": 6,
        "policy_category_count": 0,
        "log_sources": [
            {
                "type_name": "EventCRE",
                "type_id": 18,
                "name": "Custom Rule Engine-8 :: prod-qrdr01",
                "id": 63
            },
            {
                "type_name": "WindowsAuthServer",
                "type_id": 12,
                "name": "WindowsAuthServer @ PROD-WKST01",
                "id": 112
            }
        ],
        "closing_reason_id": null,
        "device_count": 2,
        "offense_type": 0,
        "relevance": 2,
        "domain_id": 0,
        "offense_source": "172.16.20.129",
        "local_destination_address_ids": [
            4
        ],
        "local_destination_count": 1,
        "status": "OPEN"
    },
    {
        "username_count": 0,
        "description": "Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges containing Success Audit: A scheduled task was updated ",
        "rules": [
            {
                "id": 100407,
                "type": "CRE_RULE"
            }
        ],
        "event_count": 28,
        "flow_count": 0,
        "assigned_to": null,
        "security_category_count": 3,
        "follow_up": false,
        "source_address_ids": [
            6
        ],
        "source_count": 1,
        "inactive": true,
        "protected": false,
        "closing_user": null,
        "destination_networks": [
            "Net-10-172-192.Net_172_16_0_0"
        ],
        "source_network": "Net-10-172-192.Net_172_16_0_0",
        "category_count": 3,
        "close_time": null,
        "remote_destination_count": 0,
        "start_time": 1638555204525,
        "magnitude": 2,
        "last_updated_time": 1638628785460,
        "credibility": 3,
        "id": 19,
        "categories": [
            "Information",
            "Misc Suspicious Event",
            "Scheduled Task Creation Success"
        ],
        "severity": 6,
        "policy_category_count": 0,
        "log_sources": [
            {
                "type_name": "EventCRE",
                "type_id": 18,
                "name": "Custom Rule Engine-8 :: prod-qrdr01",
                "id": 63
            },
            {
                "type_name": "WindowsAuthServer",
                "type_id": 12,
                "name": "WindowsAuthServer @ PROD-WKST01",
                "id": 112
            }
        ],
        "closing_reason_id": null,
        "device_count": 2,
        "offense_type": 0,
        "relevance": 0,
        "domain_id": 0,
        "offense_source": "172.16.20.129",
        "local_destination_address_ids": [
            4
        ],
        "local_destination_count": 1,
        "status": "OPEN"
    }
]

Hi @Faintiz, welcome to the community :tada:

I’m sorry to hear you’re running into trouble here. The error TypeError: Cannot read property 'map' of undefined suggests that returnData[0] doesn’t have a json property. Would returnData[0] consist of the data you have shared as your data structure?

If so, have you tried using the returnJsonArray() helper from the respective tutorial?

If not, what would console.log(returnData[0]) print for you?

Thanks for getting back to me on this one.

So you were right it didn’t have a json value because I hadn’t run it through the returnJsonArray function.

I have updated my code a little to look more like this. I had to wrap the map function in an if because I got another error, not sure if that is the best way to solve it but it seemed to work.

"Cannot invoke an object which is possibly 'null' or 'undefined'."

testoutput = this.helpers.returnJsonArray(returnData);
		console.log(testoutput[0])
		if (testoutput[0].json.map) {
		return testoutput[0].json.map(item => {
			return {
				json: item
			}
		})
		};

So now I am just left with two errors when the typescript is processed. Any idea on how to move forward with these errors? Thanks again for your support already.

error TS2349: This expression is not callable - No constituent of type ‘string | number | true | object | IDataObject | GenericValue | IDataObject’ is callable

. Referring to testoutput[0].json.map

error TS7006: Parameter ‘item’ implicitly has an ‘any’ type.

The output of that console log is the following. The ironic thing is when I just use N8N and create a function node with the code snippet mentioned in my original post it works exactly as intended?

{
  json: [
    {
      username_count: 0,
      description: 'Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges\n' +
        ' containing Success Audit: A scheduled task was updated\n',
      rules: [Array],
      event_count: 13,
      flow_count: 0,
      assigned_to: null,
      security_category_count: 3,
      follow_up: false,
      source_address_ids: [Array],
      source_count: 1,
      inactive: false,
      protected: false,
      closing_user: null,
      destination_networks: [Array],
      source_network: 'Net-10-172-192.Net_172_16_0_0',
      category_count: 3,
      close_time: null,
      remote_destination_count: 0,
      start_time: 1639764968556,
      magnitude: 3,
      last_updated_time: 1639775131611,
      credibility: 3,
      id: 20,
      categories: [Array],
      severity: 6,
      policy_category_count: 0,
      log_sources: [Array],
      closing_reason_id: null,
      device_count: 2,
      offense_type: 0,
      relevance: 1,
      domain_id: 0,
      offense_source: '172.16.20.129',
      local_destination_address_ids: [Array],
      local_destination_count: 1,
      status: 'OPEN'
    },
    {
      username_count: 0,
      description: 'Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges\n' +
        ' containing Success Audit: A scheduled task was updated\n',
      rules: [Array],
      event_count: 28,
      flow_count: 0,
      assigned_to: null,
      security_category_count: 3,
      follow_up: false,
      source_address_ids: [Array],
      source_count: 1,
      inactive: true,
      protected: false,
      closing_user: null,
      destination_networks: [Array],
      source_network: 'Net-10-172-192.Net_172_16_0_0',
      category_count: 3,
      close_time: null,
      remote_destination_count: 0,
      start_time: 1638555204525,
      magnitude: 2,
      last_updated_time: 1638628785460,
      credibility: 3,
      id: 19,
      categories: [Array],
      severity: 6,
      policy_category_count: 0,
      log_sources: [Array],
      closing_reason_id: null,
      device_count: 2,
      offense_type: 0,
      relevance: 0,
      domain_id: 0,
      offense_source: '172.16.20.129',
      local_destination_address_ids: [Array],
      local_destination_count: 1,
      status: 'OPEN'
    },
    {
      username_count: 0,
      description: 'Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges\n' +
        ' containing Success Audit: A scheduled task was updated\n',
      rules: [Array],
      event_count: 4,
      flow_count: 0,
      assigned_to: null,
      security_category_count: 2,
      follow_up: false,
      source_address_ids: [Array],
      source_count: 1,
      inactive: true,
      protected: false,
      closing_user: null,
      destination_networks: [Array],
      source_network: 'Net-10-172-192.Net_172_16_0_0',
      category_count: 2,
      close_time: null,
      remote_destination_count: 0,
      start_time: 1637870472558,
      magnitude: 3,
      last_updated_time: 1637870472665,
      credibility: 4,
      id: 18,
      categories: [Array],
      severity: 6,
      policy_category_count: 0,
      log_sources: [Array],
      closing_reason_id: null,
      device_count: 2,
      offense_type: 0,
      relevance: 0,
      domain_id: 0,
      offense_source: '172.16.20.10',
      local_destination_address_ids: [Array],
      local_destination_count: 1,
      status: 'OPEN'
    },
    {
      username_count: 1,
      description: 'Testing Rule Fired\n',
      rules: [Array],
      event_count: 6,
      flow_count: 0,
      assigned_to: 'admin',
      security_category_count: 2,
      follow_up: false,
      source_address_ids: [Array],
      source_count: 1,
      inactive: true,
      protected: false,
      closing_user: null,
      destination_networks: [Array],
      source_network: 'Net-10-172-192.Net_172_16_0_0',
      category_count: 2,
      close_time: null,
      remote_destination_count: 0,
      start_time: 1637389192420,
      magnitude: 1,
      last_updated_time: 1637389584224,
      credibility: 2,
      id: 17,
      categories: [Array],
      severity: 1,
      policy_category_count: 0,
      log_sources: [Array],
      closing_reason_id: null,
      device_count: 2,
      offense_type: 3,
      relevance: 0,
      domain_id: 0,
      offense_source: 'Administrator',
      local_destination_address_ids: [Array],
      local_destination_count: 1,
      status: 'OPEN'
    }
  ]
}

Hi @Faintiz, could you maybe share a full example leading to the error?

I’ve quickly created an example node based on this skeleton code from our documentation including your example data (I’ve only added your data and return [this.helpers.returnJsonArray(example[0])];). So my full node.ts file looks like so:

Example Node TS code
import {
	IExecuteFunctions,
} from 'n8n-core';

import {
	IDataObject,
	INodeExecutionData,
	INodeType,
	INodeTypeDescription,
} from 'n8n-workflow';

import {
	OptionsWithUri,
} from 'request';

export class FriendGrid implements INodeType {
	description: INodeTypeDescription = {
		displayName: 'FriendGrid',
		name: 'friendGrid',
		icon: 'file:friendGrid.svg',
		group: ['transform'],
		version: 1,
		description: 'Consume FriendGrid API',
		defaults: {
			name: 'FriendGrid',
			color: '#1A82e2',
		},
		inputs: ['main'],
		outputs: ['main'],
		credentials: [],
		properties: [
			// Node properties which the user gets displayed and
			// can change on the node.
		],
	};

	async execute(this: IExecuteFunctions): Promise < INodeExecutionData[][] > {

		const example = [
			[{
					"username_count": 0,
					"description": "Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges containing Success Audit: A scheduled task was updated ",
					"rules": [{
						"id": 100407,
						"type": "CRE_RULE"
					}],
					"event_count": 13,
					"flow_count": 0,
					"assigned_to": null,
					"security_category_count": 3,
					"follow_up": false,
					"source_address_ids": [
						6
					],
					"source_count": 1,
					"inactive": false,
					"protected": false,
					"closing_user": null,
					"destination_networks": [
						"Net-10-172-192.Net_172_16_0_0"
					],
					"source_network": "Net-10-172-192.Net_172_16_0_0",
					"category_count": 3,
					"close_time": null,
					"remote_destination_count": 0,
					"start_time": 1639764968556,
					"magnitude": 4,
					"last_updated_time": 1639775131611,
					"credibility": 3,
					"id": 20,
					"categories": [
						"Information",
						"Misc Suspicious Event",
						"Scheduled Task Creation Success"
					],
					"severity": 6,
					"policy_category_count": 0,
					"log_sources": [{
							"type_name": "EventCRE",
							"type_id": 18,
							"name": "Custom Rule Engine-8 :: prod-qrdr01",
							"id": 63
						},
						{
							"type_name": "WindowsAuthServer",
							"type_id": 12,
							"name": "WindowsAuthServer @ PROD-WKST01",
							"id": 112
						}
					],
					"closing_reason_id": null,
					"device_count": 2,
					"offense_type": 0,
					"relevance": 2,
					"domain_id": 0,
					"offense_source": "172.16.20.129",
					"local_destination_address_ids": [
						4
					],
					"local_destination_count": 1,
					"status": "OPEN"
				},
				{
					"username_count": 0,
					"description": "Possible UAC Bypass - A Scheduled Task Has Been Configured to Run With Highest Privileges containing Success Audit: A scheduled task was updated ",
					"rules": [{
						"id": 100407,
						"type": "CRE_RULE"
					}],
					"event_count": 28,
					"flow_count": 0,
					"assigned_to": null,
					"security_category_count": 3,
					"follow_up": false,
					"source_address_ids": [
						6
					],
					"source_count": 1,
					"inactive": true,
					"protected": false,
					"closing_user": null,
					"destination_networks": [
						"Net-10-172-192.Net_172_16_0_0"
					],
					"source_network": "Net-10-172-192.Net_172_16_0_0",
					"category_count": 3,
					"close_time": null,
					"remote_destination_count": 0,
					"start_time": 1638555204525,
					"magnitude": 2,
					"last_updated_time": 1638628785460,
					"credibility": 3,
					"id": 19,
					"categories": [
						"Information",
						"Misc Suspicious Event",
						"Scheduled Task Creation Success"
					],
					"severity": 6,
					"policy_category_count": 0,
					"log_sources": [{
							"type_name": "EventCRE",
							"type_id": 18,
							"name": "Custom Rule Engine-8 :: prod-qrdr01",
							"id": 63
						},
						{
							"type_name": "WindowsAuthServer",
							"type_id": 12,
							"name": "WindowsAuthServer @ PROD-WKST01",
							"id": 112
						}
					],
					"closing_reason_id": null,
					"device_count": 2,
					"offense_type": 0,
					"relevance": 0,
					"domain_id": 0,
					"offense_source": "172.16.20.129",
					"local_destination_address_ids": [
						4
					],
					"local_destination_count": 1,
					"status": "OPEN"
				}
			]
		];

		return [this.helpers.returnJsonArray(example[0])];

	}
}

This doesn’t throw any typescript errors for me and n8n builds fine. Executing the node also returns the expected data structure for me:

So it would be great if I could see the error first hand to make sure I’m not mixing anything up here.

1 Like

Hi @MutedJam,

So I just went through your code and realised my mistake. The difference between your code and mine was in the returnJsonArray when I was just passing the return data without selecting the first element in the array.

So my original code was:

return [this.helpers.returnJsonArray(returnData)];

Your code was:

return [this.helpers.returnJsonArray(returnData[0])];

Sorry about that I feel a bit silly because it’s quite obvious. Thanks so much for taking the time to help me out! It looks to be a great community so far :smile:

2 Likes