# Same subflow, different happening on different machines

**URL:** <https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140>\
**Category:** General\
**Created:** [2 February 2024 06:40 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140 "2024-02-02T06:40:44Z")\
**Posts on this page:** 19\
**Page:** 2

<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:** [2 February 2024 22:13 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/22 "2024-02-02T22:13:55Z")

</div>

Well the change node hasn't "worked" as the topic is not being set. Why you don't get the error message I don't know.

---

<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:** [2 February 2024 22:14 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/23 "2024-02-02T22:14:53Z")

</div>

Sorry where are you seeing that?

---

<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:** [2 February 2024 22:18 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/24 "2024-02-02T22:18:09Z")

</div>

In the image you posted, the last debug output (To Button) has not got a topic. The Change node in the subflow (#1) should set the topic.

In fact it isn't working even with the quotes.

---

<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:** [2 February 2024 22:20 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/25 "2024-02-02T22:20:38Z")

</div>

Sorry, looking at the wrong output. You have got op1 and op2 upside down in the subflow

---

<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:** [2 February 2024 22:20 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/26 "2024-02-02T22:20:59Z")

</div>

`to button` doesn't need a topic.

It is going to the `button`.

the `change` node is the subflow sets the topic on the second output.

Or that's what I believe is happening.

When the button is pressed (`going in` - looped from the button's output) it needs the `topic` set (well, really it doesn't) only so the subflow can make sure it is from the right MQTT channel,

Just saw your reply.

All good.

---

<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:** [2 February 2024 22:21 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/27 "2024-02-02T22:21:55Z")

</div>

Yeah, that is a bit of a bad thing.

I try to keep them going down with 1 at the top and 2 below...

Just wasn't ...... nice in the layout in this case.

again: good catch though.

---

<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:** [2 February 2024 22:24 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/28 "2024-02-02T22:24:24Z")

</div>

For the sake of it though:

This is two screen shots on the machine that errors.

With and without the `'` around the `power` in the `#1` `change` node of the subflow.

 ![Screenshot from 2024-02-03 09-09-12](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/66aa04a92032456a4f141675b46a42dbdb861a81.jpeg)

 ![Screenshot from 2024-02-03 09-11-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/583e2f2eb1e809474ebe8ad116f15cb5af48324d.jpeg)

The error given:

```auto
{"_msgid":"f88a6d283eb807c8","topic":"ERROR_REPORT/BedPi/RealWorldControl","_event":"node:411be8a70a813fe5","error":{"message":"Invalid JSONata expression: Cannot read properties of undefined (reading 'startsWith')","source":{"id":"c57d4af5faa4d2b6","type":"change","name":"#1","count":1}},"settings":{"input":"2024-02-02T22:10:54.075Z","input_format":"","input_tz":"Australia/Sydney","output_format":"YYYY-MM-DD HH:mm:ss","output_locale":"en_AU","output_tz":"Australia/Sydney"},"time":"2024-02-03 09:10:54","payload":{"message":"Invalid JSONata expression: Cannot read properties of undefined (reading 'startsWith')","source":{"id":"c57d4af5faa4d2b6","type":"change","name":"#1","count":1}}}

```

See the `#1` as the name of the node that made the error.

This part of the error message throws me.  
`Cannot read properties of undefined (reading 'startsWith')`

MAYBE because it is seeing an undefined variable name being used...  
But shouldn't that give a different error?  
(And.....)  
why is it failing on one machine and not the other?  
:shrug:

---

<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:** [2 February 2024 22:29 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/29 "2024-02-02T22:29:49Z")

</div>

Disconnect the MQTT Out node and replace it with another Debug node and show me what you get when you click the Inject node Button (without the quotes).

Also do one further thing for me, double click the subflow (the one in the flow we are considering) and select Edit Subflow Template and check the the #1 node in there does not have the quotes. I just want to make quite sure you are looking at the correct subflow.

Got to go now, it is late, will have another look in the morning.

---

<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:** [2 February 2024 22:55 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/30 "2024-02-02T22:55:59Z")

</div>

I have been editing the subflow (via double clicking on it and opening subflow template)

The MQTT out node to the device is _dead_ as the device is not connected.  
Which is why I have the `Online` `Offline` buttons you can see.  
On one machine the `Offline` button is not shown. (Anyway....)

I need to click the `Online` to get the subflow _awake_ and accept any input.

Quick touch base with something that may (or not) be confusing you.

The _Buttons_ are not quite as simple as you may think.

They have 4 (well 5) states.

1 - OFF/OFF  
2 - ON/OFF  
3 - ON/ON  
4 - OFF/ON  
(5 - offline)

OFF/OFF looks like this:

![Screenshot from 2024-02-03 09-43-34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b524e157de15616d9c2f9e2f2f068259a959dd74.png)

Note the background RED colour.

When the two states (the first and second part shown above) do NOT match, it is ORANGERED. **NOT** RED.  
(`font color = `) in the message.  
So when I press the _button_ it changes to what ever is needed and makes the background ORANGERED.  
Then when the MQTT message is received and _CONFIRMS_ the change, it is changed to RED.  
Makes it more realistic - IMO.  
So you **may** be getting confused with that part.

So, here's what happens - better walk through of the messages.

 ![Screenshot from 2024-02-03 09-35-39](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48f18e8614bfcf56367b9bca92d19d5f0ba7687f.png)

1 - I click the `Online` to enable the button and tell the subflow that the device is online.  
I'm not sure if there are any more messages off the top of the screen. I was focusing on showing you the MQTT message - as requested.  
2 - I press the button and you see the `X` going from the button back to the subflow  
3 - You see the `ON` message being transmitted (MQTT) with the `topic` to the device and the output to the button node making it set the _button_ to the `ON` state but with the ORANGERED background.

---

<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:** [3 February 2024 09:36 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/31 "2024-02-03T09:36:48Z")

</div>

If you look at the last debug output on that image you will see that the change node has not, in fact, worked correctly. If you add the quotes it should give you the correct topic. Why it does not show the error message I don't know. Someone who knows more about how JSONata is implemented in node red might know. There must be some difference between the two systems that causes it.

---

<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 February 2024 10:11 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/32 "2024-02-03T10:11:31Z")

</div>

Thanks for all the time you put in that.

Appreciated.

Yeah, I hope someone who knows more about JSON can explain it more.

Yeah, the `debug` node isn't getting/showing the right message.

Just the error I got confused me as I don't see the mechanics of understanding what it said and equating that to missing `'` around a variable's name.

---

<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:** [3 February 2024 10:27 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/33 "2024-02-03T10:27:52Z")

</div>

I think you said the two systems are running on different hardware. Possibly the version of JSONata available for the two systems is slightly different.

---

<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 February 2024 10:28 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/34 "2024-02-03T10:28:58Z")

</div>

Maybe.

But that's above my pay grade. (I think is the saying.)

😉

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [4 February 2024 20:35 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/35 "2024-02-04T20:35:15Z")

</div>

So the `$env(...)` function is not part of the JSONata language -- that and the context functions are available only inside the node-red runtime implementation... just fyi...

I am seeing the same error message when trying to access an env var using the bare word `power`:

```
topic & '/cmnd/power' & $env(power)

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/3491585f62c2fa579012c5cae64e5a591c18d6d3.png)

An odd message to be sure... must have something to do with the implementation of the $env function.

If you don't quote the word `power` then JSONata tries to resolve that as a value, which is 'undefined'. It then passes an undefined argument to the $env() function, which fails.

But when you quote it like `$env('power')` you are passing a string to the $env() function, which returns `undefined` if the env variable does not exist. And trying to concatenate the string `cmnd/power` & `undefined` returns a value of undefined.

So the only thing to conclude is that your two systems have different environment variables defined. The one that does NOT throw the error MUST have an env variable named "power" defined, and the other does not.

It might be safer to do something like this in your `change` node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/7958832eecb0991e7af19f3d7e58c3c1b0e3145d.png)

---

<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:** [4 February 2024 21:34 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/36 "2024-02-04T21:34:04Z")

</div>

The node is inside a subflow, the power env var is one of the subflow properties. So the var is defined and has a value in both cases. I see the same symptom as @Trying_to_learn, with the quotes it works as expected, without the quotes I see the error message. On the system that appears to be working `$env(power)` appears to return nothing. The rest of the JSONata expression works as expected.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [4 February 2024 23:13 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/37 "2024-02-04T23:13:14Z")

</div>

Ah, right, subflows operate differently. If the intention is to look up an env variable named "power", then he will definitely need some quotes -- otherwise the evaluator will pass an undefined to the $env function, which will never work.

Unless I misunderstood, and the intention is to pass in a `msg.power` property, **containing the name** of the env var to actually look up? For example, if the incoming msg has `{"power": "total"}`, then the JSONata expression would end up being `$env("total")`?

As for the odd error messages, I've looked at the `evaluateJsonataExpression()` utility function and can see no reason for the error string being returned. Node-red runtime just echos whatever error string was returned from the JSONata library. I guess that's the next place to look.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [5 February 2024 01:04 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/38 "2024-02-05T01:04:14Z")

</div>

Interesting -- it looks like this same issue came up in May of '21

> [@"TypeError: Cannot read property 'startsWith' of undefined"](https://discourse.nodered.org/t/typeerror-cannot-read-property-startswith-of-undefined/46212):
>
> I have a funtion node with many functions in it. On one function I get the following error message: "TypeError: Cannot read property 'startsWith' of undefined" What causes this message. Any help appreciated

Nick and Steve added some code to ensure that context keys are valid strings before doing a lookup, but I suspect calling `$env(undefined)` from within a jsonata expression is somehow bypassing those checks.

---

<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:** [5 February 2024 02:15 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/39 "2024-02-05T02:15:02Z")

</div>

Not quite.

In the `change` node I set up `msg.power` in an attempt to _get around_ the problem before I knew what it was.

I made a whole lot of mistakes with the subflow.

Starting with `env$( )` instead of `$env( )`.

Then I forgot the `'` around the name.

Luckily it is now working.

I hope I can put what I have been told/shown to good use.

---

<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:** [19 February 2024 02:15 UTC](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140/40 "2024-02-19T02:15:25Z")

</div>

Closed on 19 February 2024 02:15 UTC

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

[Previous page](https://discourse.nodered.org/t/same-subflow-different-happening-on-different-machines/85140.md?page=1)
