# The old topic of messagse. (msg.payload etc)

**URL:** <https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056>\
**Category:** General\
**Created:** [4 August 2018 22:46 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056 "2018-08-04T22:46:01Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 August 2018 10:15 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/21 "2018-08-05T10:15:49Z")

</div>

> [@Trying\_to\_learn](#):
>
> I used the exact code on their page:
> 
> ```auto
> <!DOCTYPE html>
> <html>
> <head>
> <title>Learn Enough JavaScript</title>
> <meta charset="utf-8">
> <script>
> alert("hello, world!");
> </script>
> </head>
> <body>
> <h1>Hello, world!</h1>
> <p>This page includes an alert written in JavaScript.</p>
> </body>
> </html>
> 
> ```

Did you copy/paste that from the docs or from your code? If from the docs then please copy/paste from your code.  
Also did you get the correct output at the previous step (without the javascript).

---

<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:** [5 August 2018 10:18 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/22 "2018-08-05T10:18:52Z")

</div>

Yes that was from the web page.

The first step worked.

I then blocked/copy and pasted to the "index.html" file. Deleting the original first.

So, for 100% clarity this is from my machine - index.html:

```auto
<!DOCTYPE html>
<html>
  <head>
    <title>Learn Enough JavaScript</title>
    <meta charset="utf-8">
    <script>
      alert("hello, world!");
    </script>
  </head>
  <body>
    <h1>Hello, world!</h1>
    <p>This page includes an alert written in JavaScript.</p>
  </body>
</html>

```

If I press the OK, the text appears.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 August 2018 10:26 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/23 "2018-08-05T10:26:25Z")

</div>

That's ok then, it is just a race condition in the browser as to which gets there first. If you refresh the browser, which is what the instructions say, do you see both?

---

<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:** [5 August 2018 10:30 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/24 "2018-08-05T10:30:22Z")

</div>

If I load the page I get the pop up.  
If I press the RELOAD button of the browser, the page reloads as the same.  
Pressing STOP, the page remains the same.

Pressing the OK on the popup, it goes to

 ![Screenshot%20from%202018-08-05%2020-27-24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/902d7f7159a5504fbcf2d2e69a62657937de1c45.png)

Pressing REFRESH again, it goes back to the popup.

Never do I see the two on the screen at once.

!!!!

If I press the STOP button the press the OK button the screen is BLANK  
Sorry, forgot that permutation.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 August 2018 10:36 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/25 "2018-08-05T10:36:20Z")

</div>

I suggest sending feedback to the author then. In fact I see the same thing.

---

<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:** [5 August 2018 10:36 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/26 "2018-08-05T10:36:56Z")

</div>

Colin,

As much as I appreciate your help, I feel this goes with the "You are wasting your time and ours" statement.

I accept that.

I have (finally) found a way to get in contact with the owner.

I'll let them help me. (Tuche. You beat me to the post.)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [12 August 2018 20:31 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/27 "2018-08-12T20:31:53Z")

</div>

> [@Trying\_to\_learn](#):
>
> **NR is a platform that allows you to "code" while not getting bogged down in the nitty gritty of the languages.**

I've not seen that statement in context, but I suspect that the author was referring to the _linking together of nodes into a working flow_ as "coding" -- also known as "flowgramming". In this sense, each node is a single-purpose program and the wires are the connections that link the output of one program to the input of the next.

So yes, you can "program" lots of solutions without writing 1 line of "code" -- but you cannot (easily) do **everything** using just pre-built node logic. Thus, the authors created the core `function` node, which is a VM (virtual machine) sandbox which can run almost any JavaScript code that we may need. Later, others have done the same thing with other real programming languages (e.g. python, java, etc) by contributing 3rd-party nodes to the [flows.nodered.org](http://flows.nodered.org) library. **BUT when you choose to use one of these programming nodes, you really need to understand the language first**.

This is where you are getting stuck, it seems... I would bet that every one of your `function` nodes could be replaced with 1 or 2 core nodes, and would greatly simplify the readability of your flows. As a rule of thumb, I like to only use core nodes to build my flows. Then if I need some special logic, I look for a contributed node in the flows library, Finally, if that doesn't exist (or the function I need is fairly simple) I write some custom JavaScript inside a function node -- but that is the exception, _not_ the first node I reach for.

The other bit of confusion (for many of us in fact) is that JavaScript (aka ECMAScript) was originally developed to add logic to web pages running in the client browser -- such as in the Hello World web page examples in this thread. But now it is also the language of the backend servers inside the node.js runtime. I daresay that learning JS syntax and datatypes by writing html pages will lead to _way more confusion_. I find it easier to just run the `node` command, and type in JS commands to test my syntax and get the output I need -- here is a sample of me running the "node" command, and trying to extract just the local time from the current Date:

```
C:\Users\stephen_rickus> node
> new Date();
2018-08-12T20:12:47.419Z
> var dtm = new Date();
undefined
> dtm.toLocalTimeString();
TypeError: dtm.toLocalTimeString is not a function
> dtm.toLocaleTimeString();
'16:13:18'

```

Using your previous msg object, we can test the `indexOf(...)` function syntax, and get it working in the command line before saving it in the function node:

```
> var msg = {"topic":"192.168.0.82","host":"192.168.0.82","_msgid":"f7757b0b.70f8c8","_topic":"MusicPi","payload":"Off-line","_event":"node:14780718.82f8a9"};
undefined
> msg.payload;
'Off-line'
> msg.payload.indexOf("line");
4
> msg.payload.indexOf("foo");
-1
> msg.payload.indexOf("line") >= 0
true
> msg.payload.indexOf("line") >= 0 ? "lime" : "brown";
'lime'

```

Just be aware that some of the things you are using (i.e. the incoming `msg` object, and the `node` object with its `status(...)` and `send(...)` methods) do not exist in plain node JS -- those are created within the function node's VM as hooks for node-red users.

---

<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:** [12 August 2018 22:32 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/29 "2018-08-12T22:32:03Z")

</div>

Thanks very much for that.

I didn't know there was a command "node".

I shall try it and use it as a better (?) test bed for any function nodes.

With respect to making custom nodes:  
I am torn between which is the better answer.  
1 - Use the supplied nodes and have "twice as many as needed" or  
2 - Write custom functions and have less nodes.

I shall have to try and get the tutorials done so I can learn more of the tricks about syntax

That is one of the main banes for me. ` ' and " and who has priority over which, and how to add "control characters" in to a string.

Looking at the three examples of code:  
(And thanks for the time to do them)  
1 - I can't see how that progressed from

```auto
new Date()

```

to

```auto
dtm.toLocaleTimeString();

```

Ok, it was hinted at with the line:

```auto
dtm.toLocalTimeString();
TypeError: dtm.toLocalTimeString is not a function

```

But the leap from that to the last is beyond my knowledge.

2 - That's kind of how I would go. Just not having the knowledge of NODE made it a bit slower.  
And noted about status( ) and send( ).

I need to learn more about the structure of the command and the (what's the term?) what goes after the . - method?

Some of the stuff I am watching they use different terms for the first part.  
I think some call it a function and others call it something else.  
(Bad example I know)

```auto
document.write("stuff")

```

Write is a "method" but document isn't given a qualifying name.

I'm looking for a good base line for what is called what in the command.

Maybe something like:

```auto
command.method (parameters)

```

---

<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:** [13 August 2018 02:54 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/30 "2018-08-13T02:54:33Z")

</div>

Not sure if I should start "another" thread but I ran into this question when chasing another problem:

```auto
{"topic":"192.168.0.83","host":"192.168.0.83","_msgid":"1b2da70a.a12099","_topic":"BedPi","payload":"192.168.0.83 On-line","_event":"node:14780718.82f8a9"}

```

This is on a DEBUG node. So nothing is happening to the data.  
the msg.topic is 192.168.0.83  
So what is this \_topic a bit further along that is "BedPi".

I am going back (upstream?) on the flow to try and find it, but just can't remember what it is (how it is set), or how to access it.

---

<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:** [13 August 2018 03:09 UTC](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056/31 "2018-08-13T03:09:03Z")

</div>

I think I found the answer.

the "\_topic":"BedPI" part is accessed by msg.\_topic.

Silly me.

All good in the end.

[Previous page](https://discourse.nodered.org/t/the-old-topic-of-messagse-msg-payload-etc/2056.md?page=1)
