Lets say i have function node that implements various api functions and I want to dynamically call those functions based on a msg property. In the browser one would call the windows[fn] but that is not available in nodejs, eval is possible, but generally not recommended. Is there better way (other than doing if/else if)?
function start(id){...}
function stop(id){...}
let fn = msg.payload.fn // start
let id = msg.payload.id
eval(`${fn}(${id})`);
Put the code into an externnal module file and load it into the global vars in your settings.js
Then you can refernce it in a function node simply by doing a global.get.
Define your functions as properties of an object so you can reference them. (Which is what the window object basically is in the browser).
const myFuncs = {
start: function(id) { ... },
stop: function(id) { ... }
}
let fn = msg.payload.fn // start
let id = msg.payload.id
myFuncs[fn](id)
Ah yeah ofcourse, the penny has dropped thanks, makes perfect sense.