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

**URL:** https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382
**Category:** General
**Created:** [16 June 2020 02:12 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382 "2020-06-16T02:12:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [16 June 2020 02:12 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382/1 "2020-06-16T02:12:33Z")

</div>

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?

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

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [16 June 2020 02:30 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382/2 "2020-06-16T02:30:48Z")

</div>

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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506). Otherwise it cannot be imported.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [16 June 2020 09:42 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382/3 "2020-06-16T09:42:52Z")

</div>

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 😀

**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

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [16 June 2020 11:57 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382/4 "2020-06-16T11:57:48Z")

</div>

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...

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [17 June 2020 14:33 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382/5 "2020-06-17T14:33:47Z")

</div>

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.

---

<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: [1 July 2020 14:33 UTC](https://discourse.nodered.org/t/code-works-in-function-but-not-in-a-change-node/28382/6 "2020-07-01T14:33:47Z")

</div>

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