Code works in function, but not in a Change Node?

I am new to node red, but been a system designer, extensive code designer on many systems, etc. Well I guess you get the picture, I know programming and many languages. But I digress.

Below are three(3) simple flows. The first one works. The second one does not, but I think it should? The third one works, but not sure it is valid or best practice, need suggestions on if it is such? Seems like a bit of a hack?

[{"id":"f06145ab.bcc268","type":"tab","label":"Test","disabled":false,"info":""},{"id":"63130af7.50e58c","type":"inject","z":"f06145ab.bcc268","name":"Test (False)","topic":"Test","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":120,"wires":[["92211433.f9a4a8"]]},{"id":"92211433.f9a4a8","type":"function","z":"f06145ab.bcc268","name":"Set Flow To Message","func":"var theMessage=flow.get('Message') || {};\n\nflow.set('Message', 'This is a test!');\nmsg.payload=flow.get('Message');\n\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":120,"wires":[["67bc5365.df0544"]]},{"id":"67bc5365.df0544","type":"debug","z":"f06145ab.bcc268","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":120,"wires":[]},{"id":"d6ce6963.ce1118","type":"inject","z":"f06145ab.bcc268","name":"Test (False)","topic":"Test","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":180,"wires":[["e6e594cd.840ff"]]},{"id":"e6e594cd.840ff","type":"change","z":"f06145ab.bcc268","name":"Set flow.payload","rules":[{"t":"set","p":"payload","pt":"flow","to":"This is a test!","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":180,"wires":[["407ec064.2ad938"]]},{"id":"407ec064.2ad938","type":"change","z":"f06145ab.bcc268","name":"Set msg.payload","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":180,"wires":[["f5414498.397d98"]]},{"id":"f5414498.397d98","type":"debug","z":"f06145ab.bcc268","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":180,"wires":[]},{"id":"bda2d04d.e10208","type":"inject","z":"f06145ab.bcc268","name":"Test (False)","topic":"Test","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":240,"wires":[["29f2e3c0.b5bc9c"]]},{"id":"29f2e3c0.b5bc9c","type":"change","z":"f06145ab.bcc268","name":"Set flow.payload","rules":[{"t":"set","p":"payload","pt":"flow","to":"This is a test!","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":240,"wires":[["13bbdc29.bfca8c"]]},{"id":"13bbdc29.bfca8c","type":"change","z":"f06145ab.bcc268","name":"Move msg.payload","rules":[{"t":"move","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":240,"wires":[["9a5021d.a158d6"]]},{"id":"9a5021d.a158d6","type":"debug","z":"f06145ab.bcc268","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":240,"wires":[]}]

Any help explaining why the second flow does not work, when I think it should work, greatly appreciated.

Please edit your message to include your flow
snippet between three backticks using the code block icon </>. See How to share code or flow json. Otherwise it cannot be imported.

Note: I fixed your flow so it imports, but go read about how to import a flow

FLOW 2
I'm going to give you a hint so you can figure out the issue yourself. The first thing to do is use lots of debug nodes, so add a debug - to the second flow - after each node so you can see what is happening to the msg. Once you do that, I think you will understand why the flow is doing what you 'told' it to do and not to what you 'want' it to do :grinning:

FLOW 3
The difference between the 'set' and the 'move' is that the 'set' makes a copy of the data while the 'move' moves the data so it is removed from the the originating location

just to add... yes set is like copy/paste and move is more like cut/paste. You need to use move if you want to replace an object with another type - eg often you may want to move msg.payload.some.property to msg.payload - if that property is (say) a number then you can't just overwrite the existing object so we need to delete it then recreate the new one...

Everyone, first, thanks for the replies, confirmed what I suspected, but was not sure. And I will make sure to inject examples correctly in the future.

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