Why my global.get undefined

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:

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

return msg;

function 2 code:

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

msg : Object

object

_msgid: "cad82d827b473b38"

payload: 0x188d8460800

topic: ""

w1: undefined

w2: undefined

w3: "3"


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.

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.

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

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;

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

[{"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.

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