# TypeError: Cannot read property 'CustomerName' of undefined

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544>\
**Category:** General\
**Created:** [26 November 2020 06:24 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544 "2020-11-26T06:24:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anguslou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anguslou/32/9212_2.png) [@anguslou](https://discourse.nodered.org/u/anguslou)\
**Post date:** [26 November 2020 06:24 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/1 "2020-11-26T06:24:59Z")

</div>

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?

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 November 2020 14:38 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/2 "2020-11-26T14:38:19Z")

</div>

try using `if ('CustomerName' in myArray){`

---

<div class="post-metadata">

**Author:** ![ibmrcruicks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibmrcruicks/32/83099_2.png) [@ibmrcruicks](https://discourse.nodered.org/u/ibmrcruicks)\
**Post date:** [26 November 2020 15:03 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/3 "2020-11-26T15:03:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anguslou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anguslou/32/9212_2.png) [@anguslou](https://discourse.nodered.org/u/anguslou)\
**Post date:** [27 November 2020 06:42 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/4 "2020-11-27T06:42:26Z")

</div>

tried but doesn't work.

---

<div class="post-metadata">

**Author:** ![anguslou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anguslou/32/9212_2.png) [@anguslou](https://discourse.nodered.org/u/anguslou)\
**Post date:** [27 November 2020 07:18 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/5 "2020-11-27T07:18:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ibmrcruicks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibmrcruicks/32/83099_2.png) [@ibmrcruicks](https://discourse.nodered.org/u/ibmrcruicks)\
**Post date:** [27 November 2020 11:00 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/6 "2020-11-27T11:00:15Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 January 2021 11:00 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-customername-of-undefined/36544/7 "2021-01-26T11:00:20Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
