# Not getting why I see this error in the code (Subflow)

**URL:** <https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189>\
**Category:** General\
**Created:** [3 April 2023 09:59 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189 "2023-04-03T09:59:46Z")\
**Posts on this page:** 18\
**Page:** 1

<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 April 2023 09:59 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/1 "2023-04-03T09:59:46Z")

</div>

This is the code from inside a node I have in a `subflow`.

When I am editing it it shows an error in line 15:  
` context.set("counter",max-1);`

I'm not seeing _the elephant_.

Code:

```auto
let counter = context.get("counter") || 0;

// Get values from $env variables.
let UP = env.get("up");
let DOWN = env.get("down");
let offline = env.get("offlinemsg");
let max = env.get("max");

//node.warn("Counter is at value " + counter);
//node.status({text: "Counter is at value " + counter});

if (msg.payload == "RESET")
{
    // Reset and allow update
    context.set("counter",max-1);
}

if (msg.payload == UP)
{
    // UP
    //node.warn("+1");
    if (counter < max)
    {
        //
        // Do things here.
        counter = counter + 1;
        node.status({text: counter});
        context.set("counter",counter);
        if (counter == max)
        {
            msg.payload = offline;
            node.status({text: "OFFLINE"});
            return msg;
        }
        // Ok
        // Therefore do/send NOTHING
        return;
    }
} else
if (msg.payload == DOWN)
{
    counter = 0;
    context.set("counter",counter);
    node.status({text: counter});
    msg.payload = true;
    return msg;
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 April 2023 10:21 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/2 "2023-04-03T10:21:22Z")

</div>

#### Use the tools the Monaco editor gives you.

![chrome_eVEZfyBQvH](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75c3ec388d7ffd6dcc4720f5a5b65e9b22b8b844.gif)

- Hovering over the squiggle clearly states that `max` should be a number.
- Hovering over `max` clearly states it is a string.
- `max` is a string because that is what `env` vars are (`env.get()` returns strings).

#### Info

While this may all seem a little picky & you could probably safely ignore the errors in the editor, you invite BUGS by not fixing it properly.

For example, later on in the code you compare `max` to various `number`s, here you are comparing strings and numbers and results can be sometimes unexpected.

Also, please use `const` unless you intent on changing the values (that too will save you from future bugs)

#### using `parseInt`

![chrome_HsJL3o3Kcf](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4103272cc510772d8c620dc42cadd778b023df4.gif)

---

<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 April 2023 10:27 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/3 "2023-04-03T10:27:36Z")

</div>

So as much as I thought I had recently worked out the finer differences between:  
`let`  
`var`  
and  
`const`  
there are other things? ☹  
So `get.env()` forces it to be a _string_.

How do I do a `get.env()` if I want a _number_?  
Or: if the variable is a _number_: how do I get access to it?

I'll try to understand the rest of what you say.

---

<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 April 2023 10:40 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/4 "2023-04-03T10:40:22Z")

</div>

So....

(Foreign node - counter)  
`node-red-contrib-counter`

```auto
[{"id":"db3a6b270e9875b4","type":"counter","z":"fbe74a055f167918","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":185,"y":130,"wires":[["fa1f32eae6eb928a"]],"l":false},{"id":"fa1f32eae6eb928a","type":"switch","z":"fbe74a055f167918","name":"count value?","property":"count","propertyType":"msg","rules":[{"t":"lt","v":"max","vt":"env"},{"t":"gte","v":"max","vt":"env"}],"checkall":"true","repair":false,"outputs":2,"x":245,"y":130,"wires":[["115ce6b7b0ecbab9"],["ec436d4dd0c415df"]],"l":false}]

```

The `switch` node seems to be letting messages go to output 2 when `msg.counter` is only `1`.  
`$max` is `5`.  
So that won't be parsed correctly either?

How would I fix that?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 April 2023 10:58 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/5 "2023-04-03T10:58:29Z")

</div>

Steve already showed you in his example: parseInt....

---

<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 April 2023 11:00 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/6 "2023-04-03T11:00:24Z")

</div>

For that instance.

Not for the `switch` node problem I just asked.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 April 2023 11:00 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/7 "2023-04-03T11:00:45Z")

</div>

> [@Trying\_to\_learn](#):
>
> How do I do a `get.env()` if I want a _number_?

👇

> [@Steve-Mcl](#):
>
> #### using `parseInt`
> 
> ![chrome_HsJL3o3Kcf](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4103272cc510772d8c620dc42cadd778b023df4.gif)

> [@Trying\_to\_learn](#):
>
> So as much as I thought I had recently worked out the finer differences between:  
> `let`  
> `var`  
> and  
> `const`  
> there are other things? ☹

No, just stick to the matra...  
"Always use `const`. if `const` doesnt work, change to `let`. Dont use `var`.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 April 2023 11:01 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/8 "2023-04-03T11:01:36Z")

</div>

> [@Trying\_to\_learn](#):
>
> Not for the `switch` node problem I just asked

Maybe you should have started a new thread for that since that is a new problem 😉

---

<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 April 2023 11:03 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/9 "2023-04-03T11:03:10Z")

</div>

It makes it nearly IMPOSSIBLE to ask a question if I don't understand ANY PART of the problem on which I am working.

The `parseInt()` fixed one part.

But doesn't seem to fix the `switch` node's problem with the `env(max)` value.

Both nodes are in the same `subflow`. So where is the line drawn?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 April 2023 11:04 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/10 "2023-04-03T11:04:49Z")

</div>

Well, with your intuitive headline it makes it impossible to give best possible advice & help. It could hide anything between the Antenna and Earth. Just use common guidelines I have seen admins giving so many times

---

<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 April 2023 11:09 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/11 "2023-04-03T11:09:53Z")

</div>

If insulting me helps, fine.

I am not understanding the situation.

I ask.

Yes, if I can word it better, I probably will/would/could get better help.

I can only deal with what I know.

I am working on a subflow.  
I have SEEN A PROBLEM.  
And so ask a question about it.

I am then told that `getenv()` returns a _string_.  
This poses ANOTHER question in the same subflow where something else isn't working.  
And as I am using `$env()` in the `switch` node, and am comparing VALUES.....

Is it the same problem/related?

# I don't know!

So I am asking.

Rather than being helped, it seems insults on my stupidity seem to be thought as better.

I am who/what I am.  
I can't ask questions to things I don't understand.

So now I seem to have a lot more problems in this subflow than I knew.  
But I accept I am good at making big holes for myself with code.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 April 2023 11:23 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/12 "2023-04-03T11:23:05Z")

</div>

> [@Trying\_to\_learn](#):
>
> The `switch` node seems to be letting messages go to output 2 when `msg.counter` is only `1`.  
> `$max` is `5`.  
> So that won't be parsed correctly either?
> 
> How would I fix that?

Try JSONata `$parseInt($env("max"))` (untested, JSONata is not my thing!)

Better still, stick to the `PURE` mantra - i.e. pass sanitised values in and use `msg.max` etc.

e.g. `msg.max = parseInt(env.get('max'))`→ switch node (compare `msg.count` to `msg.max`)

---

<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 April 2023 11:25 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/13 "2023-04-03T11:25:27Z")

</div>

This is what I have done in the mean time.

(Probably what you just said.)

```auto
[{"id":"db3a6b270e9875b4","type":"counter","z":"fbe74a055f167918","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":185,"y":130,"wires":[["85c69903d46f9030"]],"l":false},{"id":"fa1f32eae6eb928a","type":"switch","z":"fbe74a055f167918","name":"count value?","property":"count","propertyType":"msg","rules":[{"t":"lt","v":"max","vt":"msg"},{"t":"gte","v":"max","vt":"msg"}],"checkall":"true","repair":false,"outputs":2,"x":285,"y":130,"wires":[["115ce6b7b0ecbab9"],["ec436d4dd0c415df"]],"l":false},{"id":"85c69903d46f9030","type":"function","z":"fbe74a055f167918","name":"get env(max)","func":"msg.max = parseInt(env.get(\"max\"));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":235,"y":130,"wires":[["fa1f32eae6eb928a"]],"l":false}]

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 April 2023 11:43 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/14 "2023-04-03T11:43:41Z")

</div>

I was not insulting, just "trying to hint" you to make things clearer and understandable. At least for me it was becoming a collection of problems instead of keeping it structured, problem by problem. But one thing is also clear, it is your thread so let it be if that's way you prefer and if it makes you happy

---

<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 April 2023 11:51 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/15 "2023-04-03T11:51:15Z")

</div>

I get what you mean, but PLEASE try to see it from my perspective:

I am .... _a wood worker_ who is trying to _build a rocket_.

I have NO real knowledge of how things work.

I seem to be/get lucky now and then.  
I have a lot of bad/poor programming habits. Probably brought on by not really ever learning any good structures.

I don't/didn't know that `get.env()` returns _strings_.  
Maybe - somewhere - it is written.  
_MAYBE_ I have read it. I've forgotten it.  
There is nothing I know of that .... _imply_ that it returns a string.  
Although the editor _helps_: It is a given that the person can READ and (more importantly) UNDERSTAND what is written there.

Please: exclude me from that subset.

I am here now watching the subflow fail - after going through and editing the codes as suggested by @Steve-Mcl and adding the `parseInt()` where needed and finding/fixing a couple of stupid mistakes I made when writing the code.  
(That stuff happens.)

So I've _knocked quite a few_ gremlins on the head.  
But the end is little better.  
It is still spitting out rogue messages when it shouldn't be.

So I'm sitting here totally not getting what is going on.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [3 April 2023 11:52 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/16 "2023-04-03T11:52:07Z")

</div>

Okay, understood, fine with me

---

<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 April 2023 12:15 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/17 "2023-04-03T12:15:19Z")

</div>

I'll sit down and try to work it out myself.

With the help I've got with the `parseInt()` trick and other things, I hope I can work out what is going on.'

---

<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:** [17 April 2023 12:15 UTC](https://discourse.nodered.org/t/not-getting-why-i-see-this-error-in-the-code-subflow/77189/18 "2023-04-17T12:15:28Z")

</div>

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