flowcomponents icon indicating copy to clipboard operation
flowcomponents copied to clipboard

Unable to perform actions on global variables inside a function

Open jayakumarj85 opened this issue 8 years ago • 6 comments

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.

jayakumarj85 avatar Oct 22 '18 11:10 jayakumarj85

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.global or FLOW.set() or FLOW.get() uses a persistent cache - data are same for all components

petersirka avatar Oct 22 '18 12:10 petersirka

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.

jayakumarj85 avatar Oct 22 '18 13:10 jayakumarj85

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?

molda avatar Oct 22 '18 14:10 molda

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).

jayakumarj85 avatar Oct 23 '18 04:10 jayakumarj85

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);

ductran54 avatar Nov 05 '19 23:11 ductran54

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

petersirka avatar Nov 06 '19 06:11 petersirka