# Made my Node-RED crash by working with global arrays

**URL:** <https://discourse.nodered.org/t/made-my-node-red-crash-by-working-with-global-arrays/83580>\
**Category:** General\
**Tags:** uibuilder\
**Created:** [10 December 2023 15:11 UTC](https://discourse.nodered.org/t/made-my-node-red-crash-by-working-with-global-arrays/83580 "2023-12-10T15:11:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daspixel](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@daspixel](https://discourse.nodered.org/u/daspixel)\
**Post date:** [10 December 2023 15:11 UTC](https://discourse.nodered.org/t/made-my-node-red-crash-by-working-with-global-arrays/83580/1 "2023-12-10T15:11:25Z")

</div>

Hi,  
I dig around in the forum [here](https://discourse.nodered.org/t/storing-data-in-an-array/32181), copy pasted some array things and mixed it up with my own knowledge - I thought I do understand 😆.  
Node-RED crashed some parts of UIBUILDER (clearly my mistake) - now I know more about stop/start/safemode, had to ga back to my .flows.backup.js (found the better solution later)  
To prevent this for the future I decided to ask:

I get messages from the windows of my house, like "window livingroom, true" ...  
I tried to store them in a global array global.set("housewindowlist").

I do use a function node with some outputs

- one for sending updates to UIBUILDER (working)
- one should output the number of open windows (I know hw to do that)
- one should create the windowlist (a global array - to use it everywhere around)

What would be the "right" way to do it without breaking Node-RED or UIBUILDER

my steps in logic:

- define an empty array
- get my message and check if the name of the window is in the array
- if no - add the name and the status { "name": "windowXY", "status":"open" }
- if yes - manipulate the entry

(though about getting the global array in the function, paste it in a local, work with the local and if ready overwrite the global?)

I could create a switch case with all known windows easyly, but as I am thinking of extending the window sensors I would love to have a dynamic list, which would last fo some time.

I hope I did not write to much about my mission, but as I was not able to transfer the existing array examples to my needs I am aware I still have to learn a lot more 😃 for Node-RED

best regards  
Antonio

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 December 2023 19:36 UTC](https://discourse.nodered.org/t/made-my-node-red-crash-by-working-with-global-arrays/83580/2 "2023-12-10T19:36:51Z")

</div>

Not sure I've fully understood your needs but I would personally use an object rather than an array for the window list - simply because it is much easier to access via a key.

```javascript
// Grab the global var or create a new object if it doesn't exist
const housewindowlist= global.get('housewindowlist') ?? {}

if (msg.windowid) {
  // here you can either see if it already exists but this is only needed if
  // you need to add data from outside the incoming msg
  //const thisWindow = mywindows[msg.windowid]
  //if (!thisWindow) {
    // create the window object
  //}
  // If everything is in the msg, you can simply always overwrite - no need to mess around
  mywindows[msg.windowid] = msg.payload
}

// Don't forget to save the updates
global.set('housewindowlist', housewindowlist)

```

---

<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 February 2024 19:36 UTC](https://discourse.nodered.org/t/made-my-node-red-crash-by-working-with-global-arrays/83580/3 "2024-02-08T19:36:59Z")

</div>

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