# Why is this working in one function and not another?

**URL:** <https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784>\
**Category:** General\
**Tags:** function-node\
**Created:** [18 June 2024 21:05 UTC](https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784 "2024-06-18T21:05:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![strelniece](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/strelniece/32/53706_2.png) [@strelniece](https://discourse.nodered.org/u/strelniece)\
**Post date:** [18 June 2024 21:05 UTC](https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784/1 "2024-06-18T21:05:10Z")

</div>

I want to check my lap-top's battery using one NR-node (node-red-contrib-battery), acpi and upower (using exec-node) and get the data as graphs.

So far so good. But I also wanted to have different colour on the line depending on state (charging/discharging). Here I found some good help in how to [Change line colour dependent on value](https://flows.nodered.org/flow/a78ac10821112eb07fb8be8957a9f7cb)

And no problem when calling acpi, but in the two others, basically the same function just give "TypeError: Cannot convert undefined or null to object"

Here are the functions, except differnt names on constants etc, due to differnt names from function it's basically the same, and can't understand the problem. Changing msg.payload to any other value than null results in no error, but why will it accept payload: null in one instance but not the others?

Here are the code as is now, I have tried so many things so hope someone have better eyes than I have now.

result from acpi, works like a charm

```auto
msg.payload = Number(msg.payload);
const charging = flow.get("charging");
let msg2 = null;

if (charging == "Charging"){
    msg.topic = "Charging";
    msg2 = {payload: null, topic: "Discharging"};
}else{
     msg.topic = "Discharging";
     msg2 = {payload: null, topic: "Charging"};
}

return [[msg2, msg]];

```

from the Battery node:

```auto
const charging = flow.get("isCharging");
let msg2 = null;

if (charging == true){
    msg.topic = "Charging";
    msg2 = { payload: null, topic: "Discharging"};
}else{
     msg.topic = "Discharging";
     msg2 = {payload: null, topic: "Charging"};
}

return [[msg2, msg]];

```

and from upower:

```auto
msg.payload = Number(msg.payload);

const charging = flow.get("state");
let msg2 = null;

if (charging == "charging"){
    msg.topic = "Charging";
    msg2 = { payload: null, topic: 'Discharging' };
}else{
     msg.topic = "Discharging";
     msg2 = {payload: null, topic: 'Charging'};
}

return [[msg2, msg]];

```

---

<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:** [18 June 2024 21:25 UTC](https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784/2 "2024-06-18T21:25:30Z")

</div>

It may help if you also include the error you get.

---

<div class="post-metadata">

**Author:** ![strelniece](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/strelniece/32/53706_2.png) [@strelniece](https://discourse.nodered.org/u/strelniece)\
**Post date:** [18 June 2024 21:32 UTC](https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784/3 "2024-06-18T21:32:07Z")

</div>

As I said, only "TypeError: Cannot convert undefined or null to object"

The same result if I use the catch-node as well. Also the same in the log...

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [18 June 2024 22:31 UTC](https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784/4 "2024-06-18T22:31:50Z")

</div>

Is it relevant that in one of your snippets of code you have  
`if (charging == "charging"){` (lower case c)  
and in another you have  
`if (charging == "Charging"){` (upper case C) ?

Does it make any difference if you give your constant "charging" a default value?

```auto
const charging = flow.get("state") ?? "No value"

```

It is always easier to understand what's going on if we can see any error message in context, ie a screen capture. Even if the screen capture shows a message letter for letter the same as you typed, it can be helpful to see it.

---

<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:** [16 September 2024 22:32 UTC](https://discourse.nodered.org/t/why-is-this-working-in-one-function-and-not-another/88784/5 "2024-09-16T22:32:19Z")

</div>

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