TypeError: Cannot read property 'CustomerName' of undefined

I may return several items in my payload, and some of them may/ may not return a "CustomerName", and I want to save the CustomerName(s) if it exists in any item.

I am stuck in checking if "CustomerName" exist in each item, anyone can help?

[{"id":"607cd2e8.bc340c","type":"inject","z":"5a7c2d8d.481f94","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":1400,"wires":[["285cc5e5.9ca98a"]]},{"id":"285cc5e5.9ca98a","type":"function","z":"5a7c2d8d.481f94","name":"data","func":"msg.payload = [{\"Items\":[],\"Count\":0,\"ScannedCount\":307,\"ConsumedCapacity\":null},{\"Items\":[{\"KeyID\":{\"S\":\"7cd7a07d-08a7-43e9-8e13-faa7a32d8f9e\"},\"CustomerName\":{\"SS\":[\"tzu_yu\"]}}],\"Count\":1,\"ScannedCount\":307,\"ConsumedCapacity\":null},{\"Items\":[],\"Count\":0,\"ScannedCount\":307,\"ConsumedCapacity\":null}];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":1400,"wires":[["d076d9bb.eeb128","4db27d61.a121b4"]]},{"id":"d076d9bb.eeb128","type":"debug","z":"5a7c2d8d.481f94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":1360,"wires":[]},{"id":"4db27d61.a121b4","type":"function","z":"5a7c2d8d.481f94","name":"check CustomeName","func":"\nvar myArray = msg.payload;\n\n\n//for (var i = 0; i < myArray.length; i++){\n    //var type = 0;\n    if (typeof(myArray[0].Items[0].CustomerName.SS[0]) != \"undefined\"){\n        msg.payload = \"undefined\";\n    } else {\n        msg.payload = \"not undefined\";\n    }\n\n\n\n/*\nvar type = myArray[0].Items[0].CustomerName.SS[0];\n    if (type === undefined){\n        msg.payload = \"undefined\";\n    } else {\n        msg.payload = \"not undefined\";\n    }\n*/\n    \n/*\n    if (myArray[1].Items[0].hasOwnProperty(\"CustomerName\")){\n        msg.payload = \"yes\";\n    } else {\n        msg.payload = \"no\";\n    }\n    \n*/\n\n/*\n//var checktype = \"\";\nfor (var i = 0; i < myArray.length; i ++){\n    //checktype = typeof msg.payload[i].Items[0].CustomerName;\n    if (checktype !== \"undefined\"){\n        msg.payload = msg.payload.Items[i].CustomerName.SS;\n    }\n}\n*/\n\n/*\n//for (var i = 0; i < myArray.length; i ++){\n    if (myArray[1].Items[0].count > 0){\n        msg.payload = myArray[1].Items[0].length;\n    }\n*/\n\n//payload[1].Items[0]\n\n\n\nreturn msg;\n\n//payload[1].Items[0].CustomerName.SS[0]\n//if (msg.hasOwnProperty(\"motor\"))","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":1420,"wires":[["295318e9.a21178"]]},{"id":"295318e9.a21178","type":"debug","z":"5a7c2d8d.481f94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":1380,"wires":[]}]

try using if ('CustomerName' in myArray){

Template nodes can be great for emitting content if it's there --

{{#payload}}{{#Items}}{{CustomerName.SS}}{{/Items}}{{/payload}}

you can massage the output into JSON, or formatted text as you like, without the need for a function node

tried but doesn't work.

Eventually I make it by quite a complex javascript, but it will be great if a simple template node with mustache can do. I tried your code but failed to emit anything.

weird - I took your flow sample, and tacked template and debug nodes after your data function node -- and get "tzu_yu" iin the debug stream. node-red V1.2.2

[{"id":"607cd2e8.bc340c","type":"inject","z":"a0fbb97e.369618","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":140,"wires":[["285cc5e5.9ca98a"]]},{"id":"285cc5e5.9ca98a","type":"function","z":"a0fbb97e.369618","name":"data","func":"msg.payload = [{\"Items\":[],\"Count\":0,\"ScannedCount\":307,\"ConsumedCapacity\":null},{\"Items\":[{\"KeyID\":{\"S\":\"7cd7a07d-08a7-43e9-8e13-faa7a32d8f9e\"},\"CustomerName\":{\"SS\":[\"tzu_yu\"]}}],\"Count\":1,\"ScannedCount\":307,\"ConsumedCapacity\":null},{\"Items\":[],\"Count\":0,\"ScannedCount\":307,\"ConsumedCapacity\":null}];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":140,"wires":[["d076d9bb.eeb128","4db27d61.a121b4","741888d6.3adc88"]]},{"id":"d076d9bb.eeb128","type":"debug","z":"a0fbb97e.369618","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":100,"wires":[]},{"id":"4db27d61.a121b4","type":"function","z":"a0fbb97e.369618","name":"check CustomeName","func":"\nvar myArray = msg.payload;\n\n\n//for (var i = 0; i < myArray.length; i++){\n    //var type = 0;\n    if (typeof(myArray[0].Items[0].CustomerName.SS[0]) != \"undefined\"){\n        msg.payload = \"undefined\";\n    } else {\n        msg.payload = \"not undefined\";\n    }\n\n\n\n/*\nvar type = myArray[0].Items[0].CustomerName.SS[0];\n    if (type === undefined){\n        msg.payload = \"undefined\";\n    } else {\n        msg.payload = \"not undefined\";\n    }\n*/\n    \n/*\n    if (myArray[1].Items[0].hasOwnProperty(\"CustomerName\")){\n        msg.payload = \"yes\";\n    } else {\n        msg.payload = \"no\";\n    }\n    \n*/\n\n/*\n//var checktype = \"\";\nfor (var i = 0; i < myArray.length; i ++){\n    //checktype = typeof msg.payload[i].Items[0].CustomerName;\n    if (checktype !== \"undefined\"){\n        msg.payload = msg.payload.Items[i].CustomerName.SS;\n    }\n}\n*/\n\n/*\n//for (var i = 0; i < myArray.length; i ++){\n    if (myArray[1].Items[0].count > 0){\n        msg.payload = myArray[1].Items[0].length;\n    }\n*/\n\n//payload[1].Items[0]\n\n\n\nreturn msg;\n\n//payload[1].Items[0].CustomerName.SS[0]\n//if (msg.hasOwnProperty(\"motor\"))","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":160,"wires":[["295318e9.a21178"]]},{"id":"741888d6.3adc88","type":"template","z":"a0fbb97e.369618","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{#payload}}{{#Items}}{{CustomerName.SS}}{{/Items}}{{/payload}}","output":"str","x":510,"y":300,"wires":[["5de56a7.b7cc294"]]},{"id":"295318e9.a21178","type":"debug","z":"a0fbb97e.369618","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":120,"wires":[]},{"id":"5de56a7.b7cc294","type":"debug","z":"a0fbb97e.369618","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":300,"wires":[]}]