context, flowContext, globalContext help me understand pls

1,643 views
Skip to first unread message

Toshi Bass

unread,
Aug 12, 2017, 12:31:25 PM8/12/17
to Node-RED
I have always used context.global.whatever successfuly 

recently I've been using context.get and context.set    fine

example:

var count = context.get('count') || 0;
count += 1;
context.set('count',count);
msg.payload=count;
return msg;

so thought I would try flow context and global context but reading the documentation for flow context see below just doesn't work for me.

if I include var flowContext = this.context().flow;   results in "TypeError: this.context is not a function"

var count = flowContext.get('count')||0;   results in "ReferenceError: flowContext is not defined (line 4, col 38)" as does var count = globalContext.get('count')||0;

What does work is var count = flow.get('count')||0; and var count = global.get('count')||0; so is this a typo on https://nodered.org/docs/creating-nodes/context or am I missing something ?

example:

var count = flow.get('count') || 0;
count += 1;
flow.set('count',count);
msg.payload=count;
return msg;

or 

var count = global.get('count') || 0;
count += 1;
global.set('count',count);
msg.payload=count;
return msg;


Flow context

The flow-level context is shared by all nodes on a given tab.

var flowContext = this.context().flow;
var count = flowContext.get('count')||0;

Nick O'Leary

unread,
Aug 12, 2017, 1:28:15 PM8/12/17
to Node-RED

You are reading the docs about creating nodes in the runtime. I assume you're writing this code in a Function node - so you should see what the 'writing functions' docs say about accessing context.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/57f6e084-f8ec-45f9-aa9d-3ccd1e911a21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Toshi Bass

unread,
Aug 12, 2017, 2:12:04 PM8/12/17
to Node-RED
oh

Toshi Bass

unread,
Sep 19, 2017, 5:22:46 AM9/19/17
to Node-RED
OK so after reading the correct docs, I have been using flow.set and flow.get successfully but I have a question ...

up to know I have used say   var val = flow.get('ssd')||"Off";  to initiate val to Off it it doesn't exist and that's worked fine in several flows 

but I am writing a function that doesn't need a var,  the old way would be to use say context.global.ssd = context.global.ssd || 0;

whats the correct way to initiate "ssd" 

Here's my function .... as you see I don't use the var ssd at all, so would    flow.get('ssd')||"Off";    Initiate 'ssd' to "Off" it it didn't exist ? 

var ssd = flow.get('ssd')||"Off";

if((msg.payload===true||msg.payload===0) && flow.get('ssd')=="Off"){
    flow.set('ssd',"On");
    msg.payload="On";
    node.status({text:flow.get('ssd')});
    return msg;
}
if((msg.payload===false||msg.payload===0) && flow.get('ssd')=="On"){
    flow.set('ssd',"Off");    
    msg.payload="Off";
    msg.kill = true;
    node.status({text:flow.get('ssd')});
    return msg;
}


Zenofmud

unread,
Sep 19, 2017, 5:53:59 AM9/19/17
to node...@googlegroups.com
Your statement:
    var ssd = flow.get('ssd')||"Off";
Is dealing with two variables a local 'ssd' variable and a flow variable 'ssd'. Think of the ssd in 'var ssd' as being called 'x'. So you could write it as:
var x = flow.get('ssd')||"Off";

So are you trying to initialize 'x' or the flow variable 'ssd'

If you want to initialize the flow variable 'ssd' then you have to do it somewhere. You could try
     flow.set('ssd') = flow.get('ssd')||"Off";

Sent from my iPad
--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nick O'Leary

unread,
Sep 19, 2017, 6:19:41 AM9/19/17
to Node-RED Mailing List
     flow.set('ssd') = flow.get('ssd')||"Off";

No, not quite the right syntax for flow.set... try:

flow.set('ssd', flow.get('ssd')||'Off');

Nick



To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/0cc49a9d-ec08-4d3f-85cb-e89c108c1c73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Toshi Bass

unread,
Sep 19, 2017, 6:23:21 AM9/19/17
to Node-RED

Yes I agree "two variables" 

I was thinking the same thing but it doesn't work  ..  flow.set('ssd') = flow.get('ssd')||"Off";    red X Bad Assignment Expected assignment or function call bla bla

In fact my function doesn't work ether or should I say its working now but if I try to copy the function and change flow.get('ssd') to  flow.get('ssd1') the new function doesn't initiate ssd1 so I conclude .. whilst I was writing this function I initiated ssd as Off and that's persisted whilst I changed my code.

Toshi Bass

unread,
Sep 19, 2017, 6:39:43 AM9/19/17
to Node-RED
YES that's solved, thanks Nick      

context.set('ssd', context.get('ssd')||'Off');    replaces the old   context.global.ssd = context.global.ssd || 0;   and works for context, flow and global   gets.
Reply all
Reply to author
Forward
0 new messages