Accessing data in an array of arrays of unequal length

Application ID

4A47197B-AE30-FA84-FF56-0071F4010900

Expected Behavior

I have an array of arrays that looks like this example code:

    [
	[
		{
			"date_updated": 1601584607000,
			"date_created": 1609882421000,
			"name": "Abi",
			"id": "lead_P71zXeP76geAQcAnW8QupakBZhku4A2P51Zi9952ocJ"
		},
		{
			"date_updated": 1610030681000,
			"date_created": 1609438479000,
			"name": "Andreas",
			"id": "lead_fOALSgG3JWA0eB2wSUjFZzxjhmPjOCEZg3ISut2VKoD"
		},
		{
			"date_updated": 1610015456000,
			"date_created": 1608679517000,
			"name": "Aderonke",
			"id": "lead_RQ9A4QSg3kK099CFGPTFR5C5zRvDeIaIbjSoCvNBWWt"
		}
	],
	[
		{
			"date_updated": 1601584607000,
			"date_created": 1609882421000,
			"name": "Abi",
			"id": "lead_P71zXeP76geAQcAnW8QupakBZhku4A2P51Zi9952ocJ"
		},
		{
			"date_updated": 1610030681000,
			"date_created": 1609438479000,
			"name": "Andreas",
			"id": "lead_fOALSgG3JWA0eB2wSUjFZzxjhmPjOCEZg3ISut2VKoD"
		},
		{
			"date_updated": 1610015456000,
			"date_created": 1608679517000,
			"name": "Aderonke",
			"id": "lead_RQ9A4QSg3kK099CFGPTFR5C5zRvDeIaIbjSoCvNBWWt"
		}
	]
]

I am flattening the array, so it looks like this:
image

Actual Behavior

I am achieving this in a test environment ( flattenlistReal*), using this strategy:

Reproducible Test Case

But when I add it to my real method, I get

{ "code": 0, "message": "Cannot read property 'length' of undefined" }

In the real method, I most probably encounter some null values.

I have added a test but it is not working

In the real method, I need to split the logic due to execution timeout. Strangely, when I disable the block to call the second API service, I don’t get an error! Why can that be?

Hello @Andreas_Marinopoulos

I don’t know where the mistake is, but I see some bad practice using in your logic
I have created my version of this logic.

Initial:

Result:

Regard, Viktor

1 Like

Thanks @viktor.liablin, I will try this right away and revert!

For some reason I am getting this error when I try to implement your suggestion:

23:22:07 | SERVER_CODE | ERROR | [19505] TypeError: Cannot read property ‘0’ of undefined at /opt/backendless/repo/4a47197b-ae30-fa84-ff56-0071f4010900/files/servercode/CODELESS/closeImport/PRODUCTION/services/closeImport/index.js:140:525 at Array.map () at closeImport.apitoJson (/opt/backendless/repo/4a47197b-ae30-fa84-ff56-0071f4010900/files/servercode/CODELESS/closeImport/PRODUCTION/services/closeImport/index.js:139:34) at processTicksAndRejections (internal/process/task_queues.js:97:5) at async Object.executor.execute (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/tasks/executor.js:191:16) at async executeTask (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/cloud-worker.js:51:24) at async processTask (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/cloud-worker.js:77:3)

Does this mean anything to you?

It seems that the logic works and, when printing our results, it appears to be mapping the items correctly until object 462 and then it throws that error. Then next item is not undefined, it is a normal object. Processing finished in 5615.732ms so I am not timing out. Any thoughts @viktor.liablin ?

Hi @Andreas_Marinopoulos

I checked the algorithm created by @viktor.liablin and it works quite well with big amount of data.
Your error caused because you tried to access an array’s data which is undefined. I would recommend you to check the correctness of your algorithm.

Regards, Denys