Subflow and subflow in a subflow and context

While learning DB2, I have come to appreciate the use of subflows. The only thing that gives me headaches is dealing with the context.

According to Working with context : Node-RED, I can use, for example,

var color = flow.get(“$parent.color”);

That helps a lot. For more complex sites, I like to use subflows within subflows... but I can't get anywhere with a $parent.

Is there any way to access a common context across multiple levels? So that the sub-subflows can access the context of the top-level subflow, so to speak?

Something like $prent.$parent.var Or is there another way? Would that be possible, for example, via an external library with the Context API?

I already work with my own library, which allows a clean separation by context and namespaces over a maximum of 2 levels ($parent). I configure the context and namespace of the SF with env variables in the SF.

Have you tried it with $parent.$parent?

Now I'm totally perplexed.
I've just tested a simple scenario: a subflow in a subflow in a subflow, and it works perfectly with $parent.$parent.$parent.
Now I just need to figure out what's going wrong in my large flow.
Thanks.

Before I knew about env.get("NR_FLOW_NAME") I defined it in each flow’s env and passed it on in every subflow. The easiest way I found to do that was to define the same variable in each subflow as env variable and call $parent there (once) every time. For example:

  • flow (top level): env flowName = “flow name”
    • subflow (level 1): env flowName = $parent.flowName
      • subflow (level 2): env flowName = $parent.flowName
        • … and so forth

It was a good way to not worry about levels (ie. $parent.$parent.$par…) and simple enough because I only needed one variable and needed it almost everywhere for logging purposes. So depends on your usage. If you have many values or don’t need them most of the time, it will be more of a hassle.

Alternatively use global context.

2 Likes