# Parent context variable accessed in subflow's first function node yields undefined?

**URL:** <https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525>\
**Category:** General\
**Created:** [30 November 2021 16:16 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525 "2021-11-30T16:16:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 11:56 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/21 "2021-12-01T11:56:26Z")

</div>

Yepp, I agree with the approach -thanks, again! I may have been unclear in the meantime but in the beginning (see first post) I mentioned that I have a problem in that function due to this error:  
"TypeError: cannot read property 'spectrum' of undefined".

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 12:00 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/22 "2021-12-01T12:00:28Z")

</div>

I agree.

The node is trying to read the context and it isn't set.

Ok, different approach:  
Stick a `switch` node just before it and that checks if the `flow.context` is ... defined.  
Or: `not null`  
(Sorry brain shutting down as it is 22:58 local)

And so only if the `flow.context` is _valid_ will the message pass.

You could put a second output to the `switch` node and then stick code that tells you that the `context` wasn't set.

That may be a way to go.

It would (should) also stop errors happening as only if the `context` is set will the node get the message.

---

<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:** [1 December 2021 12:01 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/23 "2021-12-01T12:01:33Z")

</div>

> [@Trying\_to\_learn](#):
>
> _something_ is maybe throwing in a _rogue_ message

What is a rogue message?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 12:02 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/24 "2021-12-01T12:02:22Z")

</div>

I can't say.

But if a message happens that somehow corrupts the environment..... You need to find this message and control what is happening.

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 12:02 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/25 "2021-12-01T12:02:39Z")

</div>

That's caught as well in FileSeparate\_Diagram:

```auto
try{
msg.index=null;
var obj = JSON.parse(msg.payload);
node.warn("fxModel:"+msg.payload);
flow.set("fxModel",obj);
node.warn("ch length:"+obj.channels.length);
flow.set("channels_length",obj.channels.length);
flow.set("ch_0",obj.channels[0]);
flow.set("ch_1",obj.channels[1]);
flow.set("ch_2",obj.channels[2]);
flow.set("ch_3",obj.channels[3]);
flow.set("ch_4",obj.channels[4]);
flow.set("ch_5",obj.channels[5]);
flow.set("ch_6",obj.channels[6]);
flow.set("ch_7",obj.channels[7]);
flow.set("spectrum_bins", obj.spectrum_bins);
flow.set("slider_cp_0",flow.get("slider_0"));
flow.set("slider_cp_1",flow.get("slider_1"));
flow.set("slider_cp_2",flow.get("slider_2"));
flow.set("slider_cp_3",flow.get("slider_3"));
flow.set("slider_cp_4",flow.get("slider_4"));
flow.set("slider_cp_5",flow.get("slider_5"));
flow.set("slider_cp_6",flow.get("slider_6"));
flow.set("slider_cp_7",flow.get("slider_7"));
}catch(e){
    node.warn(e.stack);
    flow.set("channels_length",0);
} 
return msg;

```

This is not exactly the same but this shall catch if the json is not ok. But if it's ok, the object properties shall be fine.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 12:03 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/26 "2021-12-01T12:03:28Z")

</div>

Sorry, that code is way above my skill set.

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 12:06 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/27 "2021-12-01T12:06:47Z")

</div>

Never mind and thanks for all your suggestions!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 12:07 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/28 "2021-12-01T12:07:02Z")

</div>

No problems.

Good luck.

---

<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:** [1 December 2021 13:33 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/29 "2021-12-01T13:33:45Z")

</div>

> [@Trying\_to\_learn](#):
>
> I can't say.

I meant what is your definition of a rougue message?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 20:27 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/30 "2021-12-01T20:27:06Z")

</div>

Oh, gotcha!

Sorry.

Well say the code is written to parse messages of a certain syntax/structure.

Because of `link` nodes and other things, you lose track of the messages and _who is connected to whom_.

A piece of code occasionally sends out a message which isn't of that structure and isn't meant to be in this part of the code.

It arrives and.... The code - as given - spits the dummy.

Here is an example of just that happening and the _double whammy_ that had me stumped for months.

```auto
[{"id":"55752315.00485c","type":"mqtt in","z":"a1911aa3.c45be8","g":"48ec7aa8.65cb1c","name":"","topic":"TELEPI_LOCAL/tele_led/","qos":"2","datatype":"auto","broker":"1ecd3a9e.dc5bfd","x":2810,"y":350,"wires":[["305b2df2.c6b6f2","9cbdf8e0.e07d48"]]},{"id":"9cbdf8e0.e07d48","type":"switch","z":"a1911aa3.c45be8","g":"48ec7aa8.65cb1c","name":"Only rgb messages","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Stop","vt":"str"},{"t":"eq","v":"Heartbeat","vt":"str"},{"t":"cont","v":"rgb","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":3110,"y":350,"wires":[[],[],["878cc5.e3b7d338"]]},{"id":"878cc5.e3b7d338","type":"switch","z":"a1911aa3.c45be8","g":"48ec7aa8.65cb1c","name":"Not empty","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":3245,"y":350,"wires":[["185d3c73.d58054","4ceea5fb.370db4","67ec3b8f.0b4ab4"]],"l":false},{"id":"185d3c73.d58054","type":"string","z":"a1911aa3.c45be8","g":"48ec7aa8.65cb1c","name":"","methods":[{"name":"delLeftMost","params":[{"type":"str","value":"rgb,"}]},{"name":"left","params":[{"type":"num","value":"1"}]},{"name":"toInteger","params":[]}],"prop":"payload","propout":"pointer","object":"msg","objectout":"msg","x":3350,"y":350,"wires":[["13c2f958.760b9f"]]},{"id":"13c2f958.760b9f","type":"string","z":"a1911aa3.c45be8","g":"48ec7aa8.65cb1c","name":"0","methods":[{"name":"chompLeft","params":[{"type":"str","value":"rgb"}]},{"name":"delLeftMost","params":[{"type":"str","value":","}]},{"name":"delLeftMost","params":[{"type":"str","value":","}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":2750,"y":420,"wires":[["79ca3d07.dd1f6c","e014241f.0a93c8","d0bb44bf.d3a558"]]},{"id":"79ca3d07.dd1f6c","type":"function","z":"a1911aa3.c45be8","g":"48ec7aa8.65cb1c","name":"Problematic node","func":"let pointer = msg.pointer;\nlet payload = msg.payload;\n\nflow.set(\"LEDSTRIP[\"+pointer+\"]\",payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2920,"y":420,"wires":[[]]},{"id":"1ecd3a9e.dc5bfd","type":"mqtt-broker","name":"LOCAL MQTT","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthPayload":"TelePi comms UP","birthMsg":{},"closeTopic":"EOM","closePayload":"TelePi shutting DOWN","closeMsg":{},"willTopic":"EOM","willQos":"0","willPayload":"TelePi Comms FAILURE","willMsg":{},"sessionExpiry":""}]

```

Node/s of interest:  
the `only rgb messages` and the `problematic node`.

The code is now working.

Originally the third output was `otherwise`, not `contains rgb`.

The first two messages (`stop` and `heartbeat`) are valid on that topic as they are used to control things.  
(Python script: `stop` tells it to stop.)  
`heartbeat` is an ongoing message to control output.

Every day I would get a message and for a long time I simply had `problematic node` disabled.  
What it provided wasn't mission critical but would be nice to have working.

So to recap: Just about daily it would give an error if enabled and active.

The other day I did something and forced a restart of the scripts.

# There it is! The problem!

I got a _rogue message_.  
Thus I changed the third output to `contains rgb` because I didn't realise this other message would happen/appear here.

I changed it and now all things are good, and the node doesn't error.

# WEIRD BONUS!

Daily (even with that node disabled) I would get a report that my WAP was going down at 05:00 then comes back.  
DAILY!  
Today: it hasn't. So I fixed two problems with one thing.

I'll have to investigate that more. Both how the rogue message is sneaking in and how it was causing the WAP to appear to go down daily at 05:00.

But this is a 100% good example how things that you see have nothing in common can cause unexpected results.

Oh: P.S.  
The rogue message was a `new day` message that should happen at 00:00. Not 05:00.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 21:33 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/31 "2021-12-01T21:33:51Z")

</div>

Oh, quick/dumb question.

If/when this happens what do you want to happen?

- Is it ok to just let the message go and do nothing

or do you want

- Is it that the message has to be received to make something happen and so MUST be received?

Just depending on which one is needed has a profound change on what happens next.

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 21:44 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/32 "2021-12-01T21:44:32Z")

</div>

Preferably, the message shall be processed. That subflow on the picture logs a certain activity of the system and it's not nice if certain entries are missing as you don't know what happened at that point. Of course, the failure can be logged as well to put at least something in the log to indicate that we know that a certain activity did happen but we could not create a log entry for it and we just let the system go on.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 21:59 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/33 "2021-12-01T21:59:33Z")

</div>

Ok. No problems.

So the _event_ needs to get a result.

It is just that sometimes you can just let it slide. That is a very easy route, but has it's own set of limitations.

I'm out of ideas for now.

But I would be interested in developments.  
(Off topic a bit)  
I don't know if you read my big post a couple back.  
Some weird things were happening to my flows and believe me catching the cause was painful and slow.  
But you can't rule anything out. As soon as you do that you are leaving yourself in the position where something you haven't considered can really bite you when you finally discover the cause.

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 22:01 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/34 "2021-12-01T22:01:42Z")

</div>

The point one needs to get with an issue is to make it reproducible. Afterwards, it's just a matter of time to figure it out 🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 22:02 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/35 "2021-12-01T22:02:24Z")

</div>

Yup.

But even that can be painfully slow as you don't know what it is that is causing it.

When the `context` is not set/valid:  
Is there a _default_ you could use?

Is the original (what is supposed to be in the `context`) still somewhere?

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 22:06 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/36 "2021-12-01T22:06:30Z")

</div>

I can only agree but you know: baby steps... One after the other. If I looked already that far, I'd have already given up.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 December 2021 22:09 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/37 "2021-12-01T22:09:51Z")

</div>

No problem.

I am only _spit balling_.

I strongly suggest you make a log of the `context` and have it time stamped so you can see as it happens.

Then when the _error_ happens - and is logged, etc - you can compare that to the log and see what is happening.

Alas this is going to take a lot of disc space and an unknown amount of time.  
Good luck with it.

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [1 December 2021 22:10 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/38 "2021-12-01T22:10:40Z")

</div>

Yepp, I know 🙂 Thanks!

---

<div class="post-metadata">

**Author:** ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)\
**Post date:** [3 December 2021 11:18 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/39 "2021-12-03T11:18:35Z")

</div>

It seems there's an issue when changing and deploying the parent subflow. I have not yet managed to find the right sequence but today in the morning I managed to reproduce it by chance when modifying the subflow, copying it to create a new instance, modifying again, deleting the previously copied instance, etc. Strangely enough, after the deploy some instances of the parent subflow worked but one did not. I have only the relevant log that shows what happened (row numbering added by me):

1. Dec 3 08:28:58 raspberrypi Node-RED[341]: 3 Dec 08:28:58 - [warn] [function:FileSeparate\_Diagram] ch length:6
2. Dec 3 08:28:58 raspberrypi Node-RED[341]: 3 Dec 08:28:58 - [warn] [function:calculate channel sum] channels\_length:undefined
3. Dec 3 08:28:58 raspberrypi Node-RED[341]: 3 Dec 08:28:58 - [warn] [function:calculate channel sum] ch\_0:undefined
4. Dec 3 08:28:58 raspberrypi Node-RED[341]: 3 Dec 08:28:58 - [error] [function:calculate channel sum] TypeError: Cannot read property 'spectrum' of undefined

Row 1. shows that the flow context variable 'channels\_length' set in the parent subflow in function node 'FileSeparate\_Diagram' has a certain value (which is 6):

```auto
node.warn("ch length:"+obj.channels.length);
flow.set("channels_length",obj.channels.length);
flow.set("ch_0",obj.channels[0]);

```

Row 2-3. show that the same flow context variables are undefined when accessed in a subflow of the parent subflow called 'calculate channel sum':

```auto
node.warn("channels_length:"+flow.get("$parent.channels_length"));
node.warn("ch_0:"+JSON.stringify(flow.get("$parent.ch_0")));

```

Row 4. shows the error as a result of these.

Does this give a clue to you what can go wrong with the subflows?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 December 2021 11:29 UTC](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525/40 "2021-12-03T11:29:27Z")

</div>

Could it be that you haven't / aren't pre-defining stuff in the subflows?

So on initial invocation they _fail_.

But subsequently they don't because the _variable_ (context) is set.

[Previous page](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525.md?page=1)

[Next page](https://discourse.nodered.org/t/parent-context-variable-accessed-in-subflows-first-function-node-yields-undefined/54525.md?page=3)
