# I don't understand the error

**URL:** <https://discourse.nodered.org/t/i-dont-understand-the-error/58995>\
**Category:** General\
**Created:** [25 February 2022 19:53 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995 "2022-02-25T19:53:11Z")\
**Posts on this page:** 9\
**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:** [25 February 2022 19:53 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/1 "2022-02-25T19:53:11Z")

</div>

Could someone have a look at this code?

This is what I got as the error:

```auto
{"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.

```auto
// 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;
}

```

---

<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:** [25 February 2022 20:22 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/2 "2022-02-25T20:22:19Z")

</div>

> [@Trying\_to\_learn](#):
>
> `var lastval = context.get(topic)`

Probably topic should be msg.topic

---

<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:** [25 February 2022 20:22 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/3 "2022-02-25T20:22:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 February 2022 20:22 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/4 "2022-02-25T20:22:53Z")

</div>

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.

---

<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:** [25 February 2022 20:33 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/5 "2022-02-25T20:33:51Z")

</div>

Sorry, which "macro editor"?

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

---

<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:** [25 February 2022 20:40 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/6 "2022-02-25T20:40:00Z")

</div>

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

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.

---

<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:** [25 February 2022 21:58 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/7 "2022-02-25T21:58:02Z")

</div>

> [@Trying\_to\_learn](#):
>
> and make it a bit _better_ (Ha!) 😉

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.

---

<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:** [25 February 2022 23:07 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/8 "2022-02-25T23:07:24Z")

</div>

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:

```auto
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!

---

<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:** [11 March 2022 23:07 UTC](https://discourse.nodered.org/t/i-dont-understand-the-error/58995/9 "2022-03-11T23:07:34Z")

</div>

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