# What's going on here? (code and errors)

**URL:** https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783
**Category:** General
**Created:** [4 March 2025 10:52 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783 "2025-03-04T10:52:05Z")
**Posts on this page:** 6
**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: [4 March 2025 10:52 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783/1 "2025-03-04T10:52:05Z")

</div>

I'm stuck on a problem with `context` and and trying to check things.

To that end this is a bit of code I wrote

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

if (msg.payload == "x")
{
    context.set("test",1)
}
else
if (msg.payload == "y")
{
    context.set("test",0)
}
let msg.payload = x;
return msg;

```

But I'm seeing/getting errors with the `msg.payload` lines.

Errr..... Why?

See included picture:

 ![Screenshot from 2025-03-04 21-52-24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f0a19106fae63819b779b0385bbef9b56bfa9c1.png)

---

<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: [4 March 2025 10:54 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783/2 "2025-03-04T10:54:32Z")

</div>

Msg is already defined, so no need for the let

---

<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: [4 March 2025 10:55 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783/3 "2025-03-04T10:55:35Z")

</div>

Thanks.

Shows how bad I've got with being away from coding for a while.

(That solved it.)

(No button to indicate that so sorry.)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 March 2025 12:26 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783/4 "2025-03-04T12:26:44Z")

</div>

Couple of reminders.

- Use `const` instead of `let` if you aren't going to change a variable.
- Don't use `||` if your context might contain something true/false looking - such as your 0 and 1. Use `??` instead which is safer.
- Be consistent with the use of `;` - either do or don't but preferably don't mix. It will catch you out eventually.

And have fun! 😄

---

<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: [4 March 2025 12:29 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783/5 "2025-03-04T12:29:38Z")

</div>

Yeah, thanks.

That is another thing I need to instil with myself.

But I am (for now) getting rid of all the `var` and making them `let`.

Then I need to check if I can make them `const`.  
_(Which I have done with some already.)_

It is just a long painful thing, and I've been out of programming for a few months.  
😢

---

<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: [18 March 2025 12:30 UTC](https://discourse.nodered.org/t/whats-going-on-here-code-and-errors/95783/6 "2025-03-18T12:30:03Z")

</div>

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