Context retention not seeming to work

(Give me a gun and let me end this stupidity!)

I don't really get what is going on here.

I've got used to the code: (example)

var j = context.get('name') || 0;

Assign j the value of the context 'name', and if there is no value, assign it 0.

Easy.

So, I'm working on a node which needs to store a context of what is going on and then control what happens with messages.

Yeah, this is kind of overkill, but today while writing more stuff, the machine would now and then just lock up.
I had to put in blocks on flows here and there to catch the error.

I then decided to add a MANUAL BLOCK function. But it needs a GUI indication of what is doing on.

So, I needed to nest icons. (Fun there.)

Got that working - after a lot of confusion.

Now I have this block of code:
There are 4 inputs.
2 - data signals of 'on' and 'off'. They are used downstream to switch things on/off.
2 - control inputs to allow/stop the data. They modify the icons sent out. (Nesting icons)

Every time I run it the context which I thought is stored is showing as 1.

The code has old stuff in it as it was originally two nodes which I combined and I left the old code in so I can cut/paste.

I'm leaving it here to ..... "show" (?) you how I think. (Yeah, that's a stretch, but....)

So the code:

var block = context.get('block_context') || 1;
node.warn("Reading context now");
node.warn(block);
if (msg.payload == "STOP")
{
   context.set('block_context',1);
   node.warn("Blocking active");
   node.warn(context.get('block_context'));
   msg.payload = [];  //  not needed now.
} else 
if (msg.payload == "GO")
{
   context.set('block_context',0);
   node.warn("Blocking free");
   node.warn(context.get('block_context'));
   msg.payload = [];  //  not needed now.
}

if (msg.payload == "on")
{
    //if (block === 0)
    if (context.get('block_context')== 0)
    {
        msg  = {payload: '<font color = "lime" i class="fa fa-bullseye fa-2x"></i>'};
        node.warn(" ON message received and passed");
        return msg;
    }
    //else if (block == 1)
    else if (context.get('block_context')== 1)
    {
        msg = { payload: '<span class="fa-stack "><font color = "lime" <i class="fa fa-bullseye fa-stack-2x"></i><font color = "black" <i class="fa fa-ban fa-stack-1x"></i><font color = "black" <i class="fa fa-ban fa-rotate-90 fa-stack-1x"></i></span>'}
        return msg;
    }
} else if (msg.payload == "off")
{
    if (block === 0)
    {
        msg  = {payload: '<font color = "red" i class="fa fa-bullseye fa-2x"></i>'};
        node.warn(" OFF message received and passed");
        return msg;
    }
    else if (block == 1)
    {
        msg = { payload: '<span class="fa-stack "><font color = "red" <i class="fa fa-bullseye fa-stack-2x"></i><font color = "black" <i class="fa fa-ban fa-stack-1x"></i><font color = "black" <i class="fa fa-ban fa-rotate-90 fa-stack-1x"></i></span>'}
        return msg;
    }
}
//return msg;

I send it 'GO' signals and I get the initial 1 which is changed to 0.
Then I press 'GO' again. It shows the "initial" value as 1 - again. But I just set it to 0.
See above.

See picture of flow.

That says get the value from the context, and if it is 0 or false or undefined then set it to 1. The result is that this line always sets it to 1. You should have it with || 0 as you suggested initially.
If you need to distinguish the startup case then don't OR it with anything, but then test for undefined.

1 Like

STUPID ME!

I thought I would be a "smarty pants" and have a default of 1, which further down the code means it is blocked.

All I needed to do was swap the 0 and 1 for their function.

Changed the first line to || 0 and all working.

I can live with a default "on". But I was feeling "adventurous" and put || 1.

Now back to the real code and flow to apply this patch.

Woo Hoo!

(Sorry, just something else that popped to mind)

Which is "better" when checking variables:

var j = context.get('foo');
if (j........)
{
  //
}

or

if(context.get('foo').........)
{
  //
}

I'm guessing they give the same result, but I was just wondering if there is a "preferred way".

I would use the latter if you don't need to use j again.

Don't worry about the fact that you fell into the trap of context.get() || 1 we have all fallen over this one. If you want it to default to a non-zero value then you can use something like

var j = context.get('foo;)
if (j === undefined) j = 1;

I think what also really made it bad for me is I usually use TEXT.

This time it was numbers......

Text seems to be more "forgiving" with the

var j = context.get('what_ever') || "blah";

You will get exactly the same effect with text if you ever set the context var to an empty string, for example if you do

context.set('what_ever', "");

then later

var j = context.get('what_ever') || "blah";

you will find that j is "blah" not "", because the empty string is considered false whereas any other string is true.

1 Like