# Cannot get length of a global array

**URL:** <https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663>\
**Category:** General\
**Created:** [19 August 2023 15:47 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663 "2023-08-19T15:47:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [19 August 2023 15:47 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/1 "2023-08-19T15:47:35Z")

</div>

This drives me nuts. Must be a simple error somewhere...  
my function want to retrieve the length of 2 global arrays:

```auto
var se = global.get("sensors");
var sn = se.length;
var li = global.get("lights");
var ln = li.length;
var m = { payload: { "# lights": ln, "# sensors": sn }};
return (m)

```

However the returned values in the NR debugger are:

```auto
{object
  # lights: undefined
  # sensors: undefined
}

```

i used the length function in other nodes, the global arrays are defined. Whats missing here?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 August 2023 15:52 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/2 "2023-08-19T15:52:07Z")

</div>

> [@haegar33](#):
>
> the global arrays are defined

Are you sure?

Update your function to this:

```auto
const se = global.get("sensors")
const sn = se ? se.length : 0
const li = global.get("lights")
const ln = li ? li.length : 0
node.warn({
    se,
    seType: typeof se,
    seIsArray: Array.isArray(se)
    li,
    liType: typeof li,
    liIsArray: Array.isArray(li)
})
msg.payload = { "# lights": ln, "# sensors": sn }}
return msg

```

and show us what you see.

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [19 August 2023 16:02 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/3 "2023-08-19T16:02:18Z")

</div>

Good idea!  
The global variables are array of objects....  
So length should return a result???

 ![node-red](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7945e8d70a4444f80f0b1b73207b2eca481b2a0.png)

Never used the the node.warn function 😉

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 August 2023 16:05 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/4 "2023-08-19T16:05:05Z")

</div>

> [@haegar33](#):
>
> The global variables are array of objects....  
> So length should return a result???

Look closer at your screenshot.

its NOT an array - its a JS object.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98f08223562f18ff7cd58eb78e697da66dbdded3.jpeg)

e.g.

```auto
const anArray = [{}, {}, {}]
const anObject = { "prop": "value" }

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 August 2023 16:05 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/5 "2023-08-19T16:05:50Z")

</div>

If you want the number of props (keys) in the object, then use `Object.keys(se || {}).length`

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [19 August 2023 16:11 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/7 "2023-08-19T16:11:08Z")

</div>

ah yes! a classical pitfall! thanks a lot, also for the hint with the node.warn function which seems to be perfect for debugging.

Cherio!

---

<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:** [2 September 2023 16:12 UTC](https://discourse.nodered.org/t/cannot-get-length-of-a-global-array/80663/8 "2023-09-02T16:12:03Z")

</div>

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