# \`function\` node. Not seeing the results I expect

**URL:** <https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319>\
**Category:** General\
**Created:** [9 September 2019 12:50 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319 "2019-09-09T12:50:58Z")\
**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:** [9 September 2019 12:50 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319/1 "2019-09-09T12:50:58Z")

</div>

I think it is more to do with what time it is here now.

Simple thing:

values (numbers) go into a `function` node.  
If it is 70, one thing happens.  
If it is 75, another thing happens.

For testing, I also check if the value is "test".

It isn't working.

I am seeing the first `node.warn( )` and the second one.

But not the third.

And not getting an output.

Injecting 70 (number)

I just added 3 lines to set up `msg1`, `msg2` and `msg3` which has kind of helped. But not solved the problem.

```auto
[{"id":"3478f7f3.682718","type":"inject","z":"675e227d.d158b4","name":"70","topic":"","payload":"70","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1770,"y":80,"wires":[["f08765be.6acb68"]]},{"id":"8ef67943.0946e8","type":"inject","z":"675e227d.d158b4","name":"test","topic":"","payload":"TEST","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1770,"y":120,"wires":[["f08765be.6acb68"]]},{"id":"f08765be.6acb68","type":"function","z":"675e227d.d158b4","name":"Make message","func":"node.warn(\"Payload \" + msg.payload);\n\nnode.warn(\"a\");\n\n// Set up messages\nmsg1.payload = {\n event: \"Over temperature\",\n priotiry: 1\n}\n\nnode.warn(\"1\");\n\nmsg2.payload = {\n event:\"BedPi shutting down. Excess heat\",\n priority: 4\n}\nmsg3.payload = {\n DEVICE:\"BedPi\",COMMAND:\"Shutdown\",state:1\n}\n\nnode.warn(\"Got here ok\");\nvar z = parseInt(msg.payload);\nnode.warn(\"Payload \" + msg.payload);\n\nif (msg.payload == 70)\n{\n return [msg1,null,null];\n} else\nif (msg.payload == 75)\n{\n return [msg1, msg2, msg3];\n} else\nif (msg.payload == \"TEST\")\n{\n return [msg1,msg2,null];\n}","outputs":3,"noerr":0,"x":1940,"y":160,"wires":[["ac098d23.584e2","2d3ef271.888286"],["e74c05a9.5c3958"],["82f08226.354798"]],"outputLabels":["A1","Shutdown command","A4"]}]

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [9 September 2019 13:05 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319/2 "2019-09-09T13:05:02Z")

</div>

This is your function node as you posted

```auto
node.warn("Payload " + msg.payload);

node.warn("a");

// Set up messages
msg1.payload = {
    event: "Over temperature",
    priotiry: 1
}

node.warn("1");

msg2.payload = {
    event:"BedPi shutting down. Excess heat",
    priority: 4
}
msg3.payload = {
    DEVICE:"BedPi",COMMAND:"Shutdown",state:1
}

node.warn("Got here ok");
var z = parseInt(msg.payload);
node.warn("Payload " + msg.payload);

if (msg.payload == 70)
{
    return [msg1,null,null];
} else
if (msg.payload == 75)
{
    return [msg1, msg2, msg3];
} else
if (msg.payload == "TEST")
{
    return [msg1,msg2,null];
}

```

Where do you declare msg1 msg2 and msg3?

If I run it I get

```auto
09/09/2019, 14:00:59[node: Make message](http://192.168.1.10:1880/#)function : (error)

"ReferenceError: msg1 is not defined (line 6, col 1)"

```

When you say you've added 3 lines is this included in what you posted or not?  
You need to declare  
`msg1={}` etc

If I add `msg1={}` I now get the third node.warn output

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [9 September 2019 13:10 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319/3 "2019-09-09T13:10:39Z")

</div>

> [@ukmoose](#):
>
> priotiry

You also have a typo...

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 September 2019 14:41 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319/4 "2019-09-09T14:41:51Z")

</div>

What if `msg.payload` is 71 ?  
What is the purpose of `var z =...` ?

This looks like something that fits the switch node, attach some template nodes with the format you want.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [9 September 2019 14:46 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319/5 "2019-09-09T14:46:06Z")

</div>

Is it possible that the message payload is a string, rather than numeric value? i.e. "70"?

You should, as already mentioned, have a catch-all "else" as well, even if it's just to help in debugging.

---

<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:** [9 September 2019 21:10 UTC](https://discourse.nodered.org/t/function-node-not-seeing-the-results-i-expect/15319/6 "2019-09-09T21:10:03Z")

</div>

Ok, recap.

(My fault)

This is the node - after I added the "three lines";

```auto
node.warn("Payload " + msg.payload);

node.warn("a");

// Set up messages
var msg1;
var msg2;
var msg3;
msg1.payload = {
    'event': "Over temperature",
    'priotiry': 1
};

node.warn("1");

msg2.payload = {
    event:"BedPi shutting down. Excess heat",
    priority: 4
}
msg3.payload = {
    DEVICE:"BedPi",COMMAND:"Shutdown",state:1
}

node.warn("Got here ok");
var z = parseInt(msg.payload);
node.warn("Payload " + msg.payload);

if (msg.payload == 70)
{
    return [msg1,null,null];
} else
if (msg.payload == 75)
{
    return [msg1, msg2, msg3];
} else
if (msg.payload == "TEST")
{
    return [msg1,msg2,null];
}

```

Ok, I had posted the flow also, not just the node. Phew.

All those "what if. . . . . ." questions.  
Where is/are the `71` (etc) inputs?

There aren't any.

Over night while asleep this thread/post has opened another set of questions about "how to construct a message" which I know I have been down recently.

@ukmoose  
The new bit of code covers your question.  
I probably should have posted it last night, but I was just too tired.

@molesworth  
That is an interesting question on another level to which I will get back to shortly.

Message construction.  
And of course now I am looking for it, I can't find the other posts of which I am thinking.

Somewhere recently I was talking about message construction.

Basically it boiled down to showing me how to create "complex" messages.

```auto
var msg1;
msg1 = {
    payload:"Something",
    another_name:"blah" ,
}

```

But it wasn't that simple.

It was actually - from memory - more like:

```auto
var msg1;
msg1 = {
     'payload':"something", 
     'another_name':"blah"
}

```

And then there was also mentioned the `,` at the end.  
One way it was a `,` and another it was `;`

(I'm sure @knolleary was involved, but again: searches yielded nothing helpful)

Because I can't find them here I am looking a bit silly. (What else is new?)

But that has not helped with the over all problem I have with the node.

Anyway, the `function` node - as I have it sets up 3 messages to be sent back with given conditions.

I construct the messages before anything just as it seemed the better way to do things.

Then depending on what is received in the `msg.payload` depends on what 3 messages (or more so: HOW those messages) are returned.

Now @molesworth:  
Looking at the FLOW I posted, it is weird that when I am looking at it  
I see this:  
`"inject","z":"675e227d.d158b4","name":"70","topic":"","payload":"70"`  
Yeah, I know: there is more.

That threw me a bit but the next part clears it up.  
`"payloadType":"num",`

Does that answer your question?

But it erks me a bit because I have just raised a question about the colours of different things:  
strings, numbers, boolean values.  
In one _screen_ they are one (set) of colours. In another they are different. Confusing.

But I'm guessing that the _values_ are stored as strings and their _type_ is set by just that part of the code:  
`"payloadType":"num",`

Problem solved - I guess.

(update)

Ok, I goofed - again.

Rather than the three lines:

```auto
var msg1;
var msg2;
var mgs3;

```

I should have had:

```auto
var msg1 = {};
var msg2 = {};
var msg3 = {};

```

FIXED for now.
