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

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:

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;
}

Use the tools the Monaco editor gives you.

chrome_eVEZfyBQvH

  • 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 numbers, 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

So as much as I thought I had recently worked out the finer differences between:
let
var
and
const
there are other things? :frowning:
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.

So....

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

[{"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?

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

For that instance.

Not for the switch node problem I just asked.

:point_down:

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

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

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?

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

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.

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)

This is what I have done in the mean time.

(Probably what you just said.)

[{"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}]

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

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.

Okay, understood, fine with me

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.'

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