# Yet another question about function error returning a string

**URL:** <https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926>\
**Category:** General\
**Created:** [28 April 2022 14:09 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926 "2022-04-28T14:09:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [28 April 2022 14:09 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/1 "2022-04-28T14:09:54Z")

</div>

First off yes I have spent a lot of time reading the documentation on JSON and Node Red working with messages as well as trying a number of different structures within the function node but I still am unable to resolve my issue.

This is the function...and it is working fine in terms of executing the subsequent nodes...however I am getting the dreaded "Function tried to send a message of type string" in the debug window. The msg.payload getting passed in is a number:

```auto
var email={};
if (msg.payload < 30)
    {
        email = "The battery temperature is: " + msg.payload; 
        return [email, msg.topic = "CPE Battery is too cold"];
    } 
else {
        return [null, null];
     }

```

Many thanks in advance!

---

<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:** [28 April 2022 14:16 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/2 "2022-04-28T14:16:13Z")

</div>

You dont return strings, you return objects. Preferably the original `msg` object

```auto
var email={}; //<<< you created an object here
if (msg.payload < 30)
    {
        email = "The battery temperature is: " + msg.payload; //<<< then you killed in and turned it into a string
        return [email, msg.topic = "CPE Battery is too cold"];
    } 
else {
        return [null, null];
     }

```

Better...

```auto
const email = { };
if (msg.payload < 30) {
     email.payload = "The battery temperature is: " + msg.payload; 
     msg.topic = "CPE Battery is too cold"
     return [email, msg];
} else {
     return [null, null];
}

```

Better still...

```auto
if (msg.payload < 30) {
     const email = { };
     email.payload = "The battery temperature is: " + msg.payload; 
     email.topic = "CPE Battery is too cold"
     return [email, msg]; //original, unmodified message comes out of 2nd output
}

```

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [28 April 2022 14:26 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/3 "2022-04-28T14:26:12Z")

</div>

Thank you for the quick reply!!

I wish I were more adept at all of this, but with each challenge my dim lightbulb brightens a little. For my edification what does the const do that is different from var? And yes...of course your solution works perfectly!

---

<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:** [28 April 2022 14:30 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/4 "2022-04-28T14:30:32Z")

</div>

If you had used `const` originally, you would have gotten an error when you run the code - indicating you did something wrong (i.e. created an object then tried to change it to a string)

Typically, try to use a `const` 1st, then if its not right, use a `let` (var has some weirdness I wont go into)

Lastly, you might benefit from enabling the `monaco` editor in your node-red, it has additional built in help, auto complete, code snippets & hints...

(search the forum for "monaco")

---

<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:** [28 April 2022 14:57 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/5 "2022-04-28T14:57:15Z")

</div>

Also, why are you doing `return [null, null]`? That doesn't really make sense. Just don't have a `return` at all if you don't need the node to output a msg.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [28 April 2022 15:21 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/6 "2022-04-28T15:21:18Z")

</div>

Steve,

The Monaco editor is quite the install! However any assistance I can get in writing the functions will be great! Quick question for the theme though...My eyes die when looking at the sea of default white and with my previous editor I had a nice dark theme. However when trying to change the monaco editor theme following the comments in your code...I cannot find the directory you specify:

```auto
        codeEditor: {
            lib: "monaco", //can be "monaco" or "ace"
            options: { 
                //// theme - must match the file name of a theme in 
                //// packages/node_modules/@node-red/editor-client/src/vendor/monaco/dist/theme
                //// e.g. "tomorrow-night", "upstream-sunburst", "github", "my-theme", "vs"
                theme: "tomorrow-night",

```

Is there another install for the themes that I need to execute?

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [28 April 2022 15:22 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/7 "2022-04-28T15:22:30Z")

</div>

That was there from me just trying to remember that I could have a different output if I needed one. When building my project I tend to copy and paste function nodes so I don't have to start from scratch. Thank you though!

---

<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:** [28 April 2022 15:27 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/8 "2022-04-28T15:27:14Z")

</div>

Here is a list of themes...

> <https://github.com/node-red/node-red/tree/master/packages/node_modules/%40node-red/editor-client/src/vendor/monaco/dist/theme>
>
> //github.com/node-red/node-red/tree/master/packages/node\_modules/%40node-red/editor-client/src/vendor/monaco/dist/theme

PS, you can also set `codeEditor` \> `options` \> `theme` to the built in `"vs"` or `"vs-dark"`

* * *

> [@Tomadoggy](#):
>
> Is there another install for the themes that I need to execute

No, everything is built in.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [28 April 2022 15:37 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/9 "2022-04-28T15:37:54Z")

</div>

😕 Likely the vs-dark would work, but after changing the settings.js and stop/start of NR along with a browser cache dump...there is no change visible.

```auto
theme: "vs-dark",

```

Is there a method to bring in another temem from the github? Again, there was no directory

```auto
packages/node_modules/@node-red/editor-client/src/vendor/monaco/dist/theme

```

built that I can see. My previous theme is in

```auto
node-red/node_modules/@node-red-contrib-themes

```

---

<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:** [28 April 2022 15:51 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/10 "2022-04-28T15:51:59Z")

</div>

I think you are mixing up editor theme and code-editor theme.

This is the code editor theme - the monaco editor - inside a function or template node...  
 ![chrome_himc4yfUxB](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a543ce50ac168b7f171191c3582c34c367b2767.gif)

The overall node-red editor theme is set via `editorTheme` \> `theme`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3a1323ea05c804634f7381f00710dd8f7626024.png)

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [28 April 2022 15:59 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/11 "2022-04-28T15:59:45Z")

</div>

Uggg indeed! So very much do appreciate your time and expertise!

---

<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:** [12 May 2022 15:59 UTC](https://discourse.nodered.org/t/yet-another-question-about-function-error-returning-a-string/61926/12 "2022-05-12T15:59:47Z")

</div>

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