Yet another question about function error returning a string

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:

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!

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

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...

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...

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
}
1 Like

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!

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")

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.

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:

        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?

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!

Here is a list of themes...

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


No, everything is built in.

:confused: 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.

theme: "vs-dark",

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

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

built that I can see. My previous theme is in

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

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

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

1 Like

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

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