# Getting Global Variable Value

**URL:** https://discourse.nodered.org/t/getting-global-variable-value/21727
**Category:** General
**Created:** [14 February 2020 21:43 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727 "2020-02-14T21:43:50Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 21:43 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/1 "2020-02-14T21:43:50Z")

</div>

Hello,

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a6d3f4d6ef3a5d43c932eb0d67c53c3daf40bba.png)

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 !

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [14 February 2020 22:07 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/2 "2020-02-14T22:07:54Z")

</div>

> [@an93](#):
>
> 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";  
> }

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [14 February 2020 22:22 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/3 "2020-02-14T22:22:07Z")

</div>

> [@an93](#):
>
> return msg[null];

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`

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 22:30 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/4 "2020-02-14T22:30:29Z")

</div>

@Colin, yes that's a typo.

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 22:33 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/5 "2020-02-14T22:33:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [14 February 2020 22:37 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/6 "2020-02-14T22:37:46Z")

</div>

> [@an93](#):
>
> how do i call the global variable then,

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 `+`

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 22:45 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/7 "2020-02-14T22:45:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [14 February 2020 22:51 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/8 "2020-02-14T22:51:00Z")

</div>

> [@an93](#):
>
> var res2 = global.get('FAULT\_STRING');

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

3 + 4 + donut = ???

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 23:02 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/11 "2020-02-14T23:02:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [14 February 2020 23:07 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/12 "2020-02-14T23:07:43Z")

</div>

> [@an93](#):
>
> else{  
> return msg;  
> }

What is the `msg` - it's not defined.

Bedtime for me 😉

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 23:08 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/13 "2020-02-14T23:08:58Z")

</div>

its supposed to be null if the condition does not meet

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

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [14 February 2020 23:12 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/14 "2020-02-14T23:12:10Z")

</div>

> [@an93](#):
>
> Bedtime @5 on friday. LAME !!!!!!

In UK - 23:10hrs....

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 23:21 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/15 "2020-02-14T23:21:31Z")

</div>

Alright in the mean time i will try \>

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 23:49 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/16 "2020-02-14T23:49:29Z")

</div>

@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];  
}

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 February 2020 23:53 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/17 "2020-02-14T23:53:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![an93](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@an93](https://discourse.nodered.org/u/an93)
#### Post date: [14 February 2020 23:56 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/18 "2020-02-14T23:56:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 February 2020 00:02 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/19 "2020-02-15T00:02:42Z")

</div>

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

```auto
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:

```auto
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:

```auto
msg.payload = res+res1+res2;

```

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

> [@an93](#):
>
> The return should be message with individual values.

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:

```auto
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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [29 February 2020 00:15 UTC](https://discourse.nodered.org/t/getting-global-variable-value/21727/20 "2020-02-29T00:15:26Z")

</div>

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