# Why my global.get undefined

**URL:** <https://discourse.nodered.org/t/why-my-global-get-undefined/79315>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 June 2023 10:24 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315 "2023-06-20T10:24:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianbrbr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brianbrbr/32/80789_2.png) [@brianbrbr](https://discourse.nodered.org/u/brianbrbr)\
**Post date:** [20 June 2023 10:24 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315/1 "2023-06-20T10:24:11Z")

</div>

I got w1, w2, w3 in function 1, show at debug 9  
why I get undefined value w1, w2 in function 2 , but get the w3 value

## function 1 code:

```auto
global.set(["w1", "w2", "w3"], [msg.w1, msg.w2, msg.w3]);

return msg;

```

## function 2 code:

```auto
var values = global.get(["w1", "w2", "w3"]);

var w1 = values[0];

var w2 = values[1];

var w3 = values[2];

msg.w1 = w1;

msg.w2 = w2;

msg.w3 = w3;

return msg;

```

## debug8

```auto
msg : Object

object

_msgid: "cad82d827b473b38"

payload: 0x188d8460800

topic: ""

w1: undefined

w2: undefined

w3: "3"

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51966ac783d15718feaba5d2b43684495bceeee1.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/840b8a6aab76f53d75220bf946f35a990c9cbee2.png)

---

<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:** [20 June 2023 10:42 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315/2 "2023-06-20T10:42:10Z")

</div>

If you send msg to the function1 with w1 property set but without w2 and w3, the w2 and w3 are undefined because they don't exist and so will be written to the global as undefined. That's how it works.

It can be fixed in many ways but it depends on the needs of whole logic you are creating.  
For example you can store/override to global only that **w** value which exists at the incoming payload and keep others as is.

---

<div class="post-metadata">

**Author:** ![brianbrbr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brianbrbr/32/80789_2.png) [@brianbrbr](https://discourse.nodered.org/u/brianbrbr)\
**Post date:** [20 June 2023 11:27 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315/3 "2023-06-20T11:27:09Z")

</div>

Thank for your reply  
I'm trying to save multiple text input values into one global list  
so that I can publish just one message to esp8266 via mqtt  
I will separate the data in arduino ide

Now I'm debuging. The value have been saved or not. And get the undefined.  
I don't know what is the best method to creating this logic.

---

<div class="post-metadata">

**Author:** ![brianbrbr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brianbrbr/32/80789_2.png) [@brianbrbr](https://discourse.nodered.org/u/brianbrbr)\
**Post date:** [20 June 2023 13:31 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315/4 "2023-06-20T13:31:49Z")

</div>

Me and my classmates find that, every time we type something in text input others will be undefined  
so I change the function 1 and solved

```auto
if (msg.w1 != undefined) flow.set("w1", msg.w1);    
if (msg.w2 != undefined) flow.set("w2", msg.w2);
if (msg.w3 != undefined) flow.set("w3", msg.w3);
return msg;

```

---

<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:** [20 June 2023 13:50 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315/5 "2023-06-20T13:50:32Z")

</div>

As @hotNipi stated these messages arrive a different times, not in same message.

You would need to join them or save them individually as they arrive.

here are 2 examples

```auto
[{"id":"d1dc9ef0f038ad9a","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"w1","payload":"1","payloadType":"num","x":110,"y":3800,"wires":[["a6cd69d1612d319a"]]},{"id":"a6cd69d1612d319a","type":"change","z":"b9860b4b9de8c8da","name":"save w's to global","rules":[{"t":"set","p":"my_w[msg.topic]","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":3840,"wires":[[]]},{"id":"df448fc5681839d1","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"w2","payload":"2","payloadType":"num","x":110,"y":3840,"wires":[["a6cd69d1612d319a"]]},{"id":"17f28bbaa3c33f0a","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"w3","payload":"3","payloadType":"num","x":110,"y":3880,"wires":[["a6cd69d1612d319a"]]},{"id":"ca627af56bb9fc0e","type":"inject","z":"b9860b4b9de8c8da","name":"retrieve w's","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":3920,"wires":[["5f2558cf60b78b47"]]},{"id":"5f2558cf60b78b47","type":"function","z":"b9860b4b9de8c8da","name":"function 27","func":"msg = global.get(\"my_w\") || null; //retrieve w's\nglobal.set(\"my_w\", undefined); // delete w's\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":3920,"wires":[["907de674449ab434"]]},{"id":"907de674449ab434","type":"debug","z":"b9860b4b9de8c8da","name":"debug 315","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":3920,"wires":[]},{"id":"255da7d0756919fb","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"w1","payload":"1","payloadType":"num","x":110,"y":4000,"wires":[["32233a07a8b9fb33"]]},{"id":"4b0296d44c7986ef","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"w2","payload":"2","payloadType":"num","x":110,"y":4040,"wires":[["32233a07a8b9fb33"]]},{"id":"1f6c1623587ea85d","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"w3","payload":"3","payloadType":"num","x":110,"y":4080,"wires":[["32233a07a8b9fb33"]]},{"id":"32233a07a8b9fb33","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":290,"y":4040,"wires":[["f45569406ecb1fb9"]]},{"id":"f45569406ecb1fb9","type":"function","z":"b9860b4b9de8c8da","name":"function 28","func":"msg.payload = `w1 = ${msg.payload.w1}\nw2 = ${msg.payload.w2}\nw3 = ${msg.payload.w3}`\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":4040,"wires":[["7f550b09f36a87c9"]]},{"id":"7f550b09f36a87c9","type":"debug","z":"b9860b4b9de8c8da","name":"debug 316","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":4040,"wires":[]}]

```

These examples are for info, as we do not know what output you want exactly and you not know the input timings and messages.

---

<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:** [19 August 2023 13:50 UTC](https://discourse.nodered.org/t/why-my-global-get-undefined/79315/6 "2023-08-19T13:50:55Z")

</div>

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