Getting Global Variable Value

Hello,

I am having some trouble with getting the value of global variable being set, in functions. Below is the flow screenshot;

Function 4 goes:
global.set("TRIGGER",msg.payload);
var res = global.get('LENGTH');
var res1 = global.get('CABLE_DESCRIPTION');
var res2 = global.get('FAULT_STRING');
var res3 = global.get('TRIGGER')
if(res3===true){
msg.payload = "res"+"res1"+"res2";
}
else{
return msg[null];
}

Function 1 till 3 remain same with change in definition of global variable name.
global.set("LENGTH",msg.payload);
return msg;

What i am trying to achieve here if trigger goes high then capture the data and display it !!

Any help/recommendation would be appreciated !

You are trying to construct the msg.payload from strings, and not the variables that you created earlier...
example - res is a variable, "res" is a string

What does that line do? I don't think it is valid javascript, but perhaps it is some subtle syntax that I have not met so far. I wonder whether you meant to say return null

@Colin, yes that's a typo.

@Paul-Reed, how do i call the global variable then,
supposedly saying
For Ex:
global.set('LENGTH',msg.payload);
return msg

Is it supposed to be
var res = global.get('LENGTH');

You are calling the global variables correctly, but when you are trying to create the msg.payload you are referring to the variables as strings, because you have added speech marks around the variable's names.
Are res, res1 and res2 numbers because you seem to be adding (+) their values together.
If res, res1 and res2 are string values, then maybe && instead of +

Yes res and res1 are numbers, res 3 is string, tried changing it but no luck yet

global.set('TRIGGER',msg.payload);

var res = global.get('LENGTH');
var res1 = global.get('CABLE_DESCRIPTION');
var res2 = global.get('FAULT_STRING');
var res3 = global.get('TRIGGER')
//var rest = msg.payload;

if(res3===false)
{
// msg.payload = "how are you";
msg.payload = res;
}
else{
return msg;
}

Is res2 a string (it's called FAULT_STRING?) if so, it's like trying to add values like;

3 + 4 + donut = ???

global.set('TRIGGER',msg.payload);

var res = global.get('LENGTH');
var res1 = global.get('CABLE_DESCRIPTION');
var res2 = global.get('FAULT_STRING');
var trig = global.get('TRIGGER')

if(trig===false)
{

msg.payload = res;
}
else{
return msg;
}

res is float which should "var" as datatype
res 1 and 2 are strings for which i get your point about my datatype being wrong

What is the msg - it's not defined.

Bedtime for me :wink:

its supposed to be null if the condition does not meet

Bedtime @5 on friday. LAME !!!!!!

In UK - 23:10hrs....

Alright in the mean time i will try >

@Colin how can we return the message with combination of String and variable together ?
var res = global.get('LENGTH');
var res1 = global.get('CABLE_DESCRIPTION');
var res2 = global.get('FAULT_STRING');
var trig = global.get('TRIGGER')
if(trig===false)
{
msg.payload = res;
return msg;
}
else
{
return msg[null];
}

Can you clarify exactly what you want the function to return? You are getting four values from context. Describe in words what you want the result to be.

@knolleary
Got it, that helps. Thank you ! Reached my max for today but that helped.

Right at the start of this thread, you had the code:

global.set("TRIGGER",msg.payload);
var res = global.get('LENGTH');
var res1 = global.get('CABLE_DESCRIPTION');
var res2 = global.get('FAULT_STRING');
var res3 = global.get('TRIGGER')
if(res3===true){
msg.payload = "res"+"res1"+"res2";
}
else{
return msg[null];
}

As Paul pointed out, the line:

msg.payload = "res"+"res1"+"res2";

Will result in msg.payload having the value "resres1res2". If you meant to join those three variables together, you should do:

msg.payload = res+res1+res2;

You will also need a return msg at the end so it returns the message.

That doesn't help explain what format you want it to be in. I have assumed you want it to return a message with the three values joined together as a string.

If instead you want it to return a message with an object payload with the three separate values in it, you could do:

msg.payload = {
   res: res,
   res1: res1,
   res2: res2
}

You would then be able to reference the individual properties later in the flow as msg.payload.res and msg.payload.res1 etc.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.