How to use flow variabile without modifing it

Hi all, can anyone explain to me why the flow's variable created with the first inject node is modified in the other two without using the flow.set() method?

[{"id":"26a58557.18feca","type":"inject","z":"938038fa.b84288","name":"Make a flow variable","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"IdDispositivo\":8,\"SystemGroup\":\"OLCIS\",\"IP\":\"192.168.1.132\",\"ShutdownDelay\":\"0\"}","payloadType":"json","x":170,"y":100,"wires":[["9a5d3c3a.80b9f"]]},{"id":"c566479c.b8dae8","type":"function","z":"938038fa.b84288","name":"setFlow.DeviceToPayload","func":"let x = flow.get('device')||0;\nlet payload = x;\npayload.count = 0;\nreturn {payload};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":190,"y":400,"wires":[[]]},{"id":"9a5d3c3a.80b9f","type":"change","z":"938038fa.b84288","name":"","rules":[{"t":"set","p":"device","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":160,"wires":[[]]},{"id":"a3defa86.57a188","type":"inject","z":"938038fa.b84288","name":"take flow variable to use it in the msg without modifing flow variable","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":280,"y":220,"wires":[["72dde4c6.6c969c"]]},{"id":"72dde4c6.6c969c","type":"change","z":"938038fa.b84288","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"device","tot":"flow"},{"t":"set","p":"payload.count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":280,"wires":[[]]},{"id":"6560276b.544388","type":"inject","z":"938038fa.b84288","name":"Do the some but in a function","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":160,"y":340,"wires":[["c566479c.b8dae8"]]}]

Because JS objects are copied and stored by reference

Clone the message
https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html

let x = flow.get('device')||0;
let payload = RED.util.cloneMessage(x);
payload.count = 0;
return {payload};
1 Like

image
OK! I got it
now ther's a new problem

My fault typo on "Red" should be "RED"

1 Like

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