I have many functions that are used in common in multiple nodes, so I would like to write a function
node that contains all of those shared functions.
Below is a string format function like C/C++ or C #, ...
I intend to create a function
node containing common functions.
Sorry if that's a stupid handle, trying to execute this code inside a function
node:
After executing the below command, there is an error "TypeError: Cannot read property 'format' of undefined"
var util = global.get('util');
msg.payload = ("{0} {1}".util.format(str1, str2));
What do I need to change in order to be able to execute this code inside a function
node?
Thank you in advance.
This is my code:
[{"id":"28f09b25.216ca4","type":"inject","z":"aa40b590.ff1888","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":1570,"y":3060,"wires":[["2056fed4.bc5862"]]},{"id":"74d3302b.5591c","type":"debug","z":"aa40b590.ff1888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1890,"y":3060,"wires":[]},{"id":"2056fed4.bc5862","type":"function","z":"aa40b590.ff1888","name":"","func":"/*\n// First, check if it's not implemented yet\nif (!String.prototype.format) {\n String.prototype.format = function() {\n var args = arguments;\n return this.replace(/{(\\d+)}/g, function(match, number) { \n return typeof args[number] != 'undefined' ? args[number] : match;\n });\n };\n}\n*/\n\nvar str1 = 'Hello';\nvar str2 = 'Node-RED';\n//msg.payload = (\"{0} {1}\".format(str1, str2));\n\nvar util = global.get('util');\nmsg.payload = (\"{0} {1}\".util.format(str1, str2));\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1720,"y":3060,"wires":[["74d3302b.5591c"]]},{"id":"a6f5a726.d648c8","type":"inject","z":"aa40b590.ff1888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1590,"y":2980,"wires":[["df10beb2.75551"]]},{"id":"df10beb2.75551","type":"function","z":"aa40b590.ff1888","name":"Functions Utility","func":"const util = (function () {\n 'use strict';\n \n // First, check if it's not implemented yet\n if (!String.prototype.format) {\n String.prototype.format = function() {\n var args = arguments;\n return this.replace(/{(\\d+)}/g, function(match, number) { \n return typeof args[number] != 'undefined' ? args[number] : match;\n });\n };\n }\n \n return {\n format:format\n };\n \n}());\n\nglobal.set('util', util);","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1780,"y":2980,"wires":[[]]}]