Function node vs change node. I'm missing something

(look at my name)

This flow:

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

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

Pressing the inject I get this:

TypeError: Cannot set property 'COMMAND' of undefined

I'm seriously missing something.

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”}

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

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

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 long as it is valid javascript.

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

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


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.

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