I wanted to test if Node-Red would accept to pass a function as a property of the msg object and have it executed downstream in the flow. Since a function is an object in JavaScript this should work flawlessly. Well, it indeed did. However I found out that the debug node will not display the function property, not even as an object. I don´t think this is relevant as it is very unlikely that someone using Node-Red will ever pass a function inside a msg object. Just wanted to confirm that this is an expected behavior of the debug node.
This is the msg created for testing. The function was added as property msg.fn
var bl = true;
var nb = 12;
var st = "Learning Node-Red";
var ar = [2,4,6,8];
var nu = null;
var ob = {"Chapter": "Node-Red basics", lesson: 1};
var fn = function () {
var mvalue = 3 + 3;
return mvalue;
};
msg.bl = bl;
msg.nb = nb;
msg.st = st;
msg.ar = ar;
msg.nu = nu;
msg.ob = ob;
msg.fn = fn;
return msg;
The flow:
[{"id":"867979e7.294318","type":"debug","z":"aca1823a.1ad09","name":"Debug Node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":890.1000137329102,"y":167.00000190734863,"wires":[]},{"id":"f28076ee.355b88","type":"inject","z":"aca1823a.1ad09","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":134.10000228881836,"y":166.00000286102295,"wires":[["ca978ff6.64f7a"]]},{"id":"ca978ff6.64f7a","type":"function","z":"aca1823a.1ad09","name":"Modify msg object","func":"var bl = true;\nvar nb = 12;\nvar st = \"Learning Node-Red\";\nvar ar = [2,4,6,8];\nvar nu = null;\nvar ob = {\"Chapter\": \"Node-Red basics\", lesson: 1};\n\nvar fn = function () {\n var mvalue = 3 + 3;\n return mvalue;\n};\n\nmsg.bl = bl;\nmsg.nb = nb;\nmsg.st = st;\nmsg.ar = ar;\nmsg.nu = nu;\nmsg.ob = ob;\nmsg.fn = fn;\n\nreturn msg;","outputs":1,"noerr":0,"x":311.1000518798828,"y":166.00000190734863,"wires":[["5492a727.500ad8"]]},{"id":"5492a727.500ad8","type":"function","z":"aca1823a.1ad09","name":"Display msg.fn","func":"node.warn(\"Coercing to string \" + msg.fn);\nreturn msg;","outputs":1,"noerr":0,"x":519.1000556945801,"y":167.00000190734863,"wires":[["90abfdca.a389f"]]},{"id":"90abfdca.a389f","type":"function","z":"aca1823a.1ad09","name":"Execute msg.fn","func":"var a = msg.fn;\nnode.warn(\"Executing the function, results... \" + a());\nreturn msg;","outputs":1,"noerr":0,"x":713.1000595092773,"y":167.00000190734863,"wires":[["867979e7.294318"]]}]
The view of the debug side panel. Property msg.fn was not displayed.