# Trying to understand - - TypeError: Cannot read property 'length' of undefined

**URL:** https://discourse.nodered.org/t/trying-to-understand-typeerror-cannot-read-property-length-of-undefined/50168
**Category:** General
**Created:** [25 August 2021 16:17 UTC](https://discourse.nodered.org/t/trying-to-understand-typeerror-cannot-read-property-length-of-undefined/50168 "2021-08-25T16:17:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RaptorUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raptoruk/32/46988_2.png) [@RaptorUK](https://discourse.nodered.org/u/RaptorUK)
#### Post date: [25 August 2021 16:17 UTC](https://discourse.nodered.org/t/trying-to-understand-typeerror-cannot-read-property-length-of-undefined/50168/1 "2021-08-25T16:17:21Z")

</div>

I've ben trying to create a set of Yes/No type buttons which after a set timeout will default to "No" . . . I've managed to get it working but lost a lot of time to the following error: _ **TypeError: Cannot read property 'length' of undefined** _

I've gotten around this but would like to understand why the error was being generated . . .

I have some test nodes to illustrate the issue:

Failing nodes:

```auto
[{"id":"acad1cef.8f8a3","type":"inject","z":"b7065b03.85e128","name":"enable","props":[{"p":"enabled","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":310,"y":1240,"wires":[["9c356d1d.26da9"]]},{"id":"9c356d1d.26da9","type":"function","z":"b7065b03.85e128","name":"","func":"var print = { payload: msg.payload.length };\n\nprint = msg;\n\nreturn print;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1240,"wires":[["2db2248d.8c444c"]]},{"id":"2db2248d.8c444c","type":"debug","z":"b7065b03.85e128","name":"msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":1240,"wires":[]}]

```

Not failing nodes:

```auto
[{"id":"acad1cef.8f8a3","type":"inject","z":"b7065b03.85e128","name":"enable","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":310,"y":1240,"wires":[["9c356d1d.26da9"]]},{"id":"9c356d1d.26da9","type":"function","z":"b7065b03.85e128","name":"","func":"var print = { payload: msg.payload.length };\n\nprint = msg;\n\nreturn print;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1240,"wires":[["2db2248d.8c444c"]]},{"id":"2db2248d.8c444c","type":"debug","z":"b7065b03.85e128","name":"msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":1240,"wires":[]}]

```

The difference is that the failing node injects _ **msg.enabled = true** _ the not failing node injects _ **msg.payload = true** _

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [25 August 2021 16:35 UTC](https://discourse.nodered.org/t/trying-to-understand-typeerror-cannot-read-property-length-of-undefined/50168/2 "2021-08-25T16:35:18Z")

</div>

For failing case the `inject` node does not send `payload` property (msg has only `enabled` property)  
But in the following function you are trying to read `payload.length`. If property of object does not exist, you can't examine any part of it.  
In this case you cant ask the `length` of `msg.payload` because `msg` does not have `payload`

---

<div class="post-metadata">

### Author: ![RaptorUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raptoruk/32/46988_2.png) [@RaptorUK](https://discourse.nodered.org/u/RaptorUK)
#### Post date: [25 August 2021 16:44 UTC](https://discourse.nodered.org/t/trying-to-understand-typeerror-cannot-read-property-length-of-undefined/50168/3 "2021-08-25T16:44:06Z")

</div>

> [@hotNipi](#):
>
> For failing case the `inject` node does not send `payload` property (msg has only `enabled` property)  
> But in the following function you are trying to read `payload.length` .

Ahh, I see . . . I assumed that;

```auto
var print = { payload: msg.payload.length },

```

was a kind of variable type or prototype, I didn't realise it would actually look for msg.payload.length

Thanks for the explanation. Every day is a school day 👍

---

<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: [8 September 2021 16:44 UTC](https://discourse.nodered.org/t/trying-to-understand-typeerror-cannot-read-property-length-of-undefined/50168/4 "2021-09-08T16:44:51Z")

</div>

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