I don't understand the error

Could someone have a look at this code?

This is what I got as the error:

{"event":{"clientX":1017,"clientY":197,"bbox":[969,224,1123,176]},"socketid":"26L447WP_qWBf2bLAACh","_msgid":"6d14dec2dbcfa5d1","error":{"message":"Error: Invalid context key","source":{"id":"796ab18f4ae96bdf","type":"function","name":"My blocker node.  V3","count":1}},"_error":"Error: Invalid context key","settings":{"input":"2022-02-25T19:33:41.269Z","input_format":"","input_tz":"Australia/Sydney","output_format":"YYYY-MM-DD HH:mm:ss","output_locale":"en_AU","output_tz":"Australia/Sydney"},"time":"2022-02-26 06:33:41","payload":{"message":"Error: Invalid context key","source":{"id":"796ab18f4ae96bdf","type":"function","name":"My blocker node.  V3","count":1}},"topic":"ERROR_REPORT/BedPi/Machine_Health"}

invalid context key
Sorry, I don't understand.

//      The shape property can be: ring or dot.
//      The fill property can be: red, green, yellow, blue or grey

//      These are the three commands accepted.
const all = "ALL";          //  Allow all message to pass
const block = "BLOCK";      //  Block repeat message (of same topic)
const one = "ONE";          //  Allow ONE message to pass.  (may not be perfect)
const stop = "STOP";        //  Block ALL message...  ALL!
//  Get the topic used to determine which messages are to control the mode.

let CTLTopic = env.get("TOPIC");
if (CTLTopic.length < 1)
    throw"You must define a TOPIC for controlling the node";

if (msg.topic == CTLTopic)
{
    //
    //  Set how node works.
    //

    if (msg.payload == stop)
    {
        node.status({ text: "Set to stop" });
        context.set("MODE",msg.payload);
    } else if (msg.payload == block)
    {
        node.status({ text: "Set to block" });
        context.set("MODE",msg.payload);
    } else if (msg.payload == all) 
    {
        node.status({ text: "Set to allow" });
        context.set("MODE",msg.payload);
    } else if (msg.payload == one) 
    {
        context.set("previousMode",context.get("MODE"));
        node.warn(context.get("previousMode"));
        node.status({ text: "Set to once" });
        context.set("MODE",msg.payload);
    } else      //      Show valid commands.
    {
        node.warn("For control of the node's mode msg.payload must be `" + all + "` `" + block + "` or `" + stop + "` or `" + one +"`");
    };
    return;
}

//        --  Main code starts here.
//  Get topic

// 26 Feb  3 lines commented out
//var topic = msg.topic;
//if (topic == "")
//    topic = "BLANK";

//  Get last stored value from topic
var lastval = context.get(topic) || null;
//  Get new value from message
var newval = msg.payload;

//  Get status from context.
var mode = context.get("MODE");

//  =============== STOP
//else if (mode == "STOP")
if (mode == stop)
{
    return;
}
//  =============== ALL
//else if (mode == "ALL")
else if (mode == all)
{
    node.status({fill:"blue",shape:"dot",text:"All"});
    context.set(topic, newval);
    return msg;
}
//  =============== BLOCK
//if (mode == "BLOCK")
else if (mode == block)
{
    //
    //  See if the new value is different to the old value.
    //
    if (newval != lastval)
    {
       context.set(topic, newval);
       node.status({fill:"green",shape:"dot",text:"Passed"});
       return msg;
    }
    node.status({fill:"red",shape:"dot",text:"Blocked"});
    return;
}
//  =============== ONE
//else if (mode == "ONE")
else if (mode == one)
{
    //
    //  Allow only 1 message to pass
    //
//    mode = context.get("MODE");
    context.set(topic, newval);
    context.set("MODE",context.get("previousMode"));
//    context.set("MODE","BLOCK");
    node.status({ text: "Set to " + context.get("previousMode") });
    return msg;
}

Probably topic should be msg.topic

1 Like

And once more, if you use the Monaco editor, it would tell you this.

I would take a look at the values of the var topic, it is used in some of your context.set()'s. The error is saying the key in your context set's are not correct. I don't see you setting topic anywhere in the code.

Sorry, which "macro editor"?

(Just got up and the machines have been having a picnic overnight and it is messy.) :frowning:

Yeah, I am TRYING to go through the code I wrote/write and make it a bit better (Ha!) :wink:

Rather than throughout the code access context stuff, I've been told/shown that you define all your external variables at the top (well, bottom in assembler, but then order doesn't matter for that) and use the new names in the code.

That keeps things local and if ever an external link changes, you only have to change it at/in ONE place.

The only exception to this is .... well: that's still a grey area for me.
And I am (probably) not doing it correctly.

Thanks for mentioning that, I probably wouldn't have spotted it soon enough.

(both)
I'll step back and see if I can get a better handle on what's going on.

Monaco editor will help you with that.

Search the forum. I've mentioned it to you before. It is MUCH better that the function editor you are currently using in node-red. Is just a setting in settings.js.

1 Like

Steve,

Thanks.

I will try to look at it.

Just now - here in Oz - someone has pissed on the string/rope and the internet is basically useless.

This is what has happened to my uplink today alone:

2022-02-26 02:00:01 120.156.11.63
2022-02-26 02:20:31 1.145.103.123
2022-02-26 02:21:01 1.145.103.123
2022-02-26 02:21:41 120.156.55.44
2022-02-26 02:26:41 1.145.103.123
2022-02-26 02:27:01 1.145.103.123
2022-02-26 02:30:01 120.156.79.182
2022-02-26 02:56:39 101.191.8.49
2022-02-26 03:02:31 120.156.50.80
2022-02-26 03:21:23 120.156.76.153
2022-02-26 03:50:32 120.156.20.164
2022-02-26 04:39:54 120.156.60.48
2022-02-26 04:46:04 120.156.67.7
2022-02-26 05:04:07 120.156.69.53
2022-02-26 05:14:42 60.229.41.164
2022-02-26 05:28:04 120.156.13.212
2022-02-26 05:42:23 120.156.42.76
2022-02-26 05:48:04 1.145.80.107
2022-02-26 05:48:34 1.145.80.107
2022-02-26 05:51:03 120.156.52.81
2022-02-26 06:35:35 1.145.76.99
2022-02-26 06:37:18 120.156.49.120
2022-02-26 07:13:45 120.156.31.207
2022-02-26 07:35:18 101.191.4.174
2022-02-26 07:45:50 120.156.62.96
2022-02-26 08:10:58 120.156.31.43

And the speed makes dialup look FAST!

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