Unable to perform actions on global variables inside a function
I am unable to set the global variable from within a function. When I try the following, the global variables are not getting set,
global.set("hello", "world"); send("Test");
global["hello"] = "world"; send("Test");
The global variable is getting set properly only if I do the following,
global["hello"] = "world"; send(global);
I don't think send(global) is the right way to set the global variables, as I might need to pass other data to the next component.
Hi, I'm not author of this component but you need to use:
// Write
global.set('key', 'value');
// is alias to: FLOW.set();
// Read
global.get('key');
// is alias to: FLOW.get();
And your problem... If you want to pass another data to another component then you can use repository object (it's a part of FlowData message), but instead of Function use Code component with:
// value {Object} contains received data
// send(outputIndex, newValue) sends a new value
// instance {Object} a current component instance
// flowdata {Object} a current flowdata
// repository {Object} a current repository of flowdata
// Example:
// send() can be execute multiple times
repository.key = 'value';
send(0, value);
And another component can read data easily:
instance.on('data', function(response) {
console.log(response.repository.key);
});
Good to know:
Function.globalorFLOW.set()orFLOW.get()uses a persistent cache - data are same for all components
Thanks, But global.set('key', 'value'); does not seem to work.
Do you mean that Function.global / FLOW.set() both are global?
My requirement is to have the global variable set by one flow execution to be available for the other flow execution.
Hi, i've just tried the Function component with bellow code
global.set('key', 'value');
send('Test);
and then read it in another Function component using
var d = global.get('key');
send(d);
It works as it should.
How do you read that value and in which component?
Thanks, getting a specific key from the global object works.
But what I have tried in my sample is to try sending the entire global object to the debug component using send(global).
In Flow, I added a global variable (e.g. my-custom-number (number) : 320.34), and it showed in the variables.txt. However, I could not get the global parameter value in the Function block using either global.get() or FLOW.get() below. What am I missing here? Thanks, var data = global.get('my-custom-number'); send(data);
FLOW.get() and FLOW.set() is in-memory persisent cache. Variables are accessible via FLOW.variables['your-key'].
FLOW.variables;
// Returns all custom variables defined by user key/value
// returns {Object}
// +v3.0.0