# Function node vs change node. I'm missing something

**URL:** https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888
**Category:** General
**Created:** [26 January 2020 22:58 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888 "2020-01-26T22:58:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 January 2020 22:58 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/1 "2020-01-26T22:58:12Z")

</div>

(look at my name)

This flow:

```auto
[{"id":"35d675e4.2ad77a","type":"function","z":"184dc884.7aba5f","name":"","func":"delete msg.payload;\nmsg.payload.COMMAND = \"Command\";\nreturn msg;","outputs":1,"noerr":0,"x":220,"y":3430,"wires":[[]]},{"id":"633c46c.32c0438","type":"inject","z":"184dc884.7aba5f","name":"","topic":"","payload":"blah","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":3390,"wires":[["35d675e4.2ad77a","7baa8ebf.dbb1c8"]]},{"id":"7baa8ebf.dbb1c8","type":"change","z":"184dc884.7aba5f","name":"","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.command","pt":"msg","to":"This is the command","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":3390,"wires":[["66d94b85.c264bc"]]}]

```

Via the change node it works. (learned what has been said)

For the sake of **trying** I thought I did the same in the function node.

This is in the node:

```auto
delete msg.payload;
msg.payload.COMMAND = "Command";
return msg;

```

Pressing the `inject` I get this:

```auto
TypeError: Cannot set property 'COMMAND' of undefined

```

Shrug.  
I'm seriously missing something.

---

<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: [26 January 2020 23:47 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/2 "2020-01-26T23:47:20Z")

</div>

`delete msg.payload` deletes ‘payload’ frpm the msg object. Since payload no longer exists, you can’t set payload.command to anything since payload does not exist..

You should be able to say `msg.payload = {“COMMAND”:”Command”}

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 January 2020 23:49 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/3 "2020-01-26T23:49:04Z")

</div>

But to me that is exactly what the `change` node is doing.

I **delete `msg.payload`** (Ok, that didn't work as I thought with formatting)  
and then set the `msg.payload.command` to the text.

Where's the elephant?

![Screenshot from 2020-01-27 10-49-26](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/9549e16174e21265e57a203680adab784d73d42d.png)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [26 January 2020 23:52 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/4 "2020-01-26T23:52:22Z")

</div>

The Change node automatically creates missing objects in the property hierarchy ... As I believe I said in your previous thread about the change node.

---

<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: [26 January 2020 23:59 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/5 "2020-01-26T23:59:29Z")

</div>

Andrew think of it this way. A `change` node does things behind the covers to make life easier for you but you are constrained by the actions the node will allow you. On the other hand, with a `function` node you have to do all the work, but you have all the control to do what you want...as long as it is valid javascript.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [27 January 2020 00:01 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/6 "2020-01-27T00:01:31Z")

</div>

Thanks Nic. I am not trying/wanting to be difficult.

I am probably just at "information overload" just now.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [27 January 2020 00:02 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/7 "2020-01-27T00:02:31Z")

</div>

Thanks.

I am really not having a good time just now with information up take.

I think I had better take a break from it and clear my head.

---

<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: [26 March 2020 22:58 UTC](https://discourse.nodered.org/t/function-node-vs-change-node-im-missing-something/20888/8 "2020-03-26T22:58:18Z")

</div>

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