# Why does this working flow generate an error if array is only 1 element in length?

**URL:** <https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605>\
**Category:** General\
**Created:** [1 October 2020 05:10 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605 "2020-10-01T05:10:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [1 October 2020 05:10 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/1 "2020-10-01T05:10:06Z")

</div>

The following flow works, no error...

```auto
[{"id":"2821116c.38475e","type":"ui_dropdown","z":"abfc01b.53d8e8","name":"Add","label":"Sessions","tooltip":"","place":"Sessions","group":"2795ce03.741602","order":1,"width":6,"height":1,"passthru":true,"multiple":false,"options":[{"label":"0","value":"{\"Session\":0,\"Zone\":0,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"1","value":"{\"Session\":1,\"Zone\":1,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"2","value":"{\"Session\":2,\"Zone\":2,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"3","value":"{\"Session\":3,\"Zone\":3,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"4","value":"{\"Session\":4,\"Zone\":4,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"}],"payload":"","topic":"","x":510,"y":1100,"wires":[["3efcb411.abdd3c"]]},{"id":"3efcb411.abdd3c","type":"debug","z":"abfc01b.53d8e8","name":"Message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":1100,"wires":[]},{"id":"b792249a.d7f168","type":"inject","z":"abfc01b.53d8e8","name":"Test","props":[{"p":"options","v":"[{\"5\":\"Test\"},{\"6\":\"Test 2\"}]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":370,"y":1100,"wires":[["2821116c.38475e"]]},{"id":"2795ce03.741602","type":"ui_group","z":"","name":"Schedule","tab":"17746764.02edd9","order":1,"disp":true,"width":6,"collapse":true},{"id":"17746764.02edd9","type":"ui_tab","z":"","name":"Irrigation","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

But the same flow with only 1 element in the array generates an error...

```auto
[{"id":"4127c5a3.cdf79c","type":"ui_dropdown","z":"6f7dafdf.3374e","name":"Add","label":"Sessions","tooltip":"","place":"Sessions","group":"2795ce03.741602","order":1,"width":6,"height":1,"passthru":true,"multiple":false,"options":[{"label":"0","value":"{\"Session\":0,\"Zone\":0,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"1","value":"{\"Session\":1,\"Zone\":1,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"2","value":"{\"Session\":2,\"Zone\":2,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"3","value":"{\"Session\":3,\"Zone\":3,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"},{"label":"4","value":"{\"Session\":4,\"Zone\":4,\"Sunday\":true,\"Monday\":true,\"Tuesday\":true,\"Wednesday\":true,\"Thursday\":true,\"Friday\":true,\"Saturday\":true,\"Instances\":1,\"Duration\":4,\"Time\":\"06:00\"}","type":"str"}],"payload":"","topic":"","x":810,"y":300,"wires":[["7af39bcc.4eeca4"]]},{"id":"7af39bcc.4eeca4","type":"debug","z":"6f7dafdf.3374e","name":"Message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":960,"y":300,"wires":[]},{"id":"80da868.abca478","type":"inject","z":"6f7dafdf.3374e","name":"Test","props":[{"p":"options","v":"[{\"5\":\"Test\"}]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":670,"y":300,"wires":[["4127c5a3.cdf79c"]]},{"id":"2795ce03.741602","type":"ui_group","z":"","name":"Schedule","tab":"17746764.02edd9","order":1,"disp":true,"width":6,"collapse":true},{"id":"17746764.02edd9","type":"ui_tab","z":"","name":"Irrigation","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

Error...  
9/30/2020, 10:07:47 PM[node: Add](http://redacted:1880/#)msg : error

"TypeError: Cannot read property 'toString' of null"

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 October 2020 06:39 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/2 "2020-10-01T06:39:42Z")

</div>

Both flows produce errors for me , this because msg.payload is null, try setting msg.payload to 0

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [1 October 2020 16:03 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/3 "2020-10-01T16:03:47Z")

</div>

That is not the issue. The issue is that if only 1 element in the array, the error results. If the array has 2 elements, no error. Something being parsed weird?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 October 2020 16:13 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/4 "2020-10-01T16:13:53Z")

</div>

Both flows, as imported, produce that error for me.

As soon as I edit the Inject nodes to set `msg.payload` to something, the error goes away.

So as far as I can tell, the bug is nothing to do with the length of the array - its whether `msg.payload` exists or not.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 October 2020 16:19 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/5 "2020-10-01T16:19:56Z")

</div>

As said i get error with both your flows.

 ![Screenshot_20201001-171452](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c55e08ae620fc47687e454266249506dab1b8c6.png)

If i give msg.payload a value the errors vanish. The problem is the drop down node throws an error if the pass though message is ticked, the node is trying to read tostring of a null value as there is no msg.payload. A possible bug in the drop down node.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [1 October 2020 17:50 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/6 "2020-10-01T17:50:13Z")

</div>

Ah, got catcha. Funny the flows worked for me when I created them, no sure how or why they did not for you.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 October 2020 19:49 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/7 "2020-10-01T19:49:21Z")

</div>

Which version of dashboard is this ?

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [2 October 2020 18:21 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/8 "2020-10-02T18:21:10Z")

</div>

2.23.2? Should be latest. Just not at home right now to confirm.

---

<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:** [1 December 2020 18:21 UTC](https://discourse.nodered.org/t/why-does-this-working-flow-generate-an-error-if-array-is-only-1-element-in-length/33605/9 "2020-12-01T18:21:12Z")

</div>

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