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

I have been sent to this site a lot:
Working with messages

Nice.

But I am looking at stuff and I am reading about:
msg.payload.shift()

Mr G sends me to that site (seems to be a very popular one) but I can't find shift() mentioned on that page.

Ok, I am being "pedantic" (maybe) in that I need to "go deeper" on that page.

But from where I am (knowledge wise) I am standing in front of 1,00000.... doors.
Is there somewhere I can go and enter (for instance) msg.payload.shift and get an explanation of how to use it?
(Any command actually would be better.)

That way when I am trying to claw my way through the commands: every time I find a new one and want to use it, it doesn't become an entirely new quest.
23 windows, 510 tabs is a LOT to get through as it is. Adding more just complicates things.

Before you touch any more Node-RED, you need go take one (or maqybe more) tutorials on javascript and work thru them until you are very comfortable with Javascript. Then you can come back and use that knowledge while you try to write functions using javascript in Node-RED

So these commands are Javascript?

If only I got that earlier.

It would have saved a lot of time.

(And I have shot myself in the foot with the RBE thread too.)

I know this is about to go off topic, but please indulge me this question:

I accept I am not off my "L Plates" with NR.
But I have done a bit.

While sort of taking on board what you said, this problem that has now just happened throws me for 6 to what is going on.

I am wanting to learn more and .... (oh boy is it difficult to explain what I am thinking.)

I have a node working. Dunno if it is 100% "by the rules" but it seems to do what it is intended to do.
So, if it works, I can use bits of it to help with other things I want to do.

Forgive the blabbering.
This is the code:
(Look for the .indexOf( ) part.)

//  Commnad return scanner.
//
//  New STATE values:
//  0 - Command good.  Use stored colour background.
//  1 - Button pressed.  Set colour background.
//  2 - Command received.  Set colour background.
//  3 - Command error.  Set colour background.
//  4 - Device state changed.  Update colour background.
var state;
var device_name = msg.topic;
var result = msg.payload;

msg.device_ID = msg.topic;

if (result == "Command received")
{
    //  Signal command received
    node.status({fill:"black",shape:"dot",text:"Received"});
    msg.state = 2;
    msg.background = "black";
} else
if (result == "Shutdown/Restart Command received")
{
    //  No more replies expected from machine.
    node.status({fill:"brown",shape:"dot",text:"No Reply expected"});
    msg.state = 0;
    msg.background = "lime";
} else
//if (result .indexOf('{"code":0}') !== -1)
if (result .indexOf(':0}') !== -1)
{
    //  Good command
    node.status({fill:"green",shape:"dot",text:"Good Command"});
    msg.state = 0;
    msg.background = "lime";
} else
//if (result .indexOf('code":127') !== -1)
if (result .indexOf(':127}') !== -1)
{
  //  Code 127.  Error.
  node.status({fill:"red",shape:"dot",text:"Error"});
  //msg.state = 4;
  msg.state = 3;
  msg.background = "red";
}
return msg;

So it gets the payload and puts it into variable called result.

var result = msg.payload;

Then further down it is:

if (result .indexOf(':0}') !== -1)

Now, I (hand on heart) don't know HOW it works, but it seems to.
I had to jiggle the syntax (as you can probably see from the line above it in the code.

But: suffice to say if it sees a colon and a zero, (successful command) it branches.
Be that right or wrong, there is no error on that node.

I take that node (or more so that line and put it in a new node:

var result = msg.payload;
if (result .indexOf('line') !== -1)
{
    //
    node.status({fill:"yellow",shape:"dot",text:"Found"});

}
var colour = (msg.payload === "On-line")?"lime":"brown";
//node.warn("Selected colour is " + colour);
var last = context.get('last') || "blank";
//node.warn("The previous message was " + last);
if (colour !== last)
{
    node.warn("They don't match");
    var msg2 = { topic:colour,payload:"", background:colour, device_ID:"MusicPi", state:4};
    context.set('last',colour);
    node.status({fill:"green",shape:"dot",text:colour});
    return msg2
} else
{
    node.status({fill:"red",shape:"dot",text:colour});
}

What happens?

TypeError: result.indexOf is not a function.

HOW CAN THAT BE?

I am probably (and again) making a stupid mistake. I don't know why/how I am doing it, but I guess I shall have to put up with this until I learn.

I can't see anything wrong. (Famous last words.)

Any help? Any one?

it means indexOf() is not a function of your variable result. It means result is not an array nor a string. These two types are the ones that have an indexOf() function "built-in".

Please verifiy the contents of result. For example by using a debug Node or a line like console.log(result);

See also:


1 Like

Thanks.

I am noticing that I have to keep moving the line with where I ask questions.
Let me explain:

NR (as I understand) is supposed to be a language to stratify programming, and rather than having to get bogged down in the "nitty gritty" of languages, you use NR to be a layer higher.

Yeah, I know that is a bit generalistic.
I'm stuck with how to search "msg.payload" (or some string) for something.
Searching Javascript commands (which I have been told are basically what I need to use in this case) for "search sting for substring" you get pointed to commands like
.indexOf( )
But I tried about 6 of them, all with the same result: not a function.

So I am confused to "how to word the questions" even for JavaScript to get helpful responses?
Being told to use indexOf( ) when that is specifically for an array or a "string".

That too gets me...... Searching for "line" in "Off-line" or "On-line" and it says it can't because "it" isn't a string.

Then what is "Off-line" and "On-line" if they aren't strings?

Sorry. That isn't directed to you.
I am declaring my confusion.

When you are writing code in the function node you are writing JavaScript.

To write JavaScript you need to be armed with some knowledge about the language, as well as the data you are working with.

If you try calling a function on a message property and you get a 'not a function' error, then that message property is not the type you think it is. You can then use the debug node it identify the type of a particular property.

So if you are trying to use indexOf on a property and it says 'not a function', then that property is not a String or Array.

Use a debug node to identify what it is.

Well, from the flow, and a debug node I get this output:

{"topic":"192.168.0.82","host":"192.168.0.82","_msgid":"f7757b0b.70f8c8","_topic":"MusicPi","payload":"Off-line","_event":"node:14780718.82f8a9"}

So from Mr G asking "Javascript what is a string" I get this:
A JavaScript string stores a series of characters like "John Doe". A string can be any text inside double or single quotes: var carname = "Volvo XC60"; var carname = 'Volvo XC60'; String indexes are zero-based: The first character is in position 0, the second in 1, and so on.

So reading that:
"Off-line"
is a string.

But I am being told by the program that it isn't.

I agree that payload is a string.

So is it possible that sometimes your flow passes through a message with a different payload?

Here's a tip... Immediately before you try treating the payload as a string, add the following line:

node.warn(typeof msg.payload);

That will cause the node to log to the debug sidebar the exact type of the msg.payload it is handling.

1 Like

It looks like you have an extra space in your code...

Thanks Nick.

Another handy tool/command.

Just - also - sometimes the debug sidebar is ...... scrolling like crazy .... with messages.
Yes, I know I could turn off the other nodes. I guess that is par for the course. But sometimes I am getting node.warn( ) messages too.

I would direct the output to the console, but the main NR machine is remote. So I don't know an alternative on how to catch specific node outputs.

(slightly off topic but to do with capturing outputs:
All those catch nodes and their flows are really not helping. As much as they tell me the flow and the error, it doesn't help me find THE NODE that sourced it.
And, if I remove the catch nodes and click on the error message part to supposedly show me the node: I get taken to a random part of the flow. Usually "in the middle of no where". That isn't helping me find problems. Sorry. Should I make this a separate thread?)

Are you seriously telling us that you did not know that what you are writing in a function node is Javascript? Surely you have intently studied the node red docs page on writing functions [1]. In particular the first line is helpful.

I strongly endorse @zenofmud's recommendation to work through a decent tutorial. This guy's tutorials are usually good though I have not used this one [2]. I think it is free to use online but if you find it useful you might feel morally obliged to pay for the download and the additional resources.

Seriously, take a few days out from node red and work through the tutorial, that way you will, in the long run, save yourself and us a lot of time and stress.

[1] Redirecting…
[2] https://www.learnenough.com/javascript-tutorial

I'm glad you mentioned that.

Yeah, that is a contentious issue.

Should it be:
if (result.indexOf('line') !== -1)

or

if (result .indexOf(.......

With the space between the result and the .indexOf(

Reading the 'net it is the first one.
But I have a node with the space and I have tested it, sent variable messages to it testing the different IF statements.

It works.
So I am stuck at the "if it ain't broke, don't fix it". Though I am worried Gremlins are breeding waiting to give me future problems.

Colin.

Honestly?

No, I know "JS" about JavaScript.

As I have mentioned (here or another thread?) and I am not casting aspersions against anyone in saying this.

NR is a platform that allows you to "code" while not getting bogged down in the nitty gritty of the languages.

(Not an exact quote, but anyway.)

And I get there are times when you DO need to get into the dirty bits to get things done.
With ABS(NO) knowledge I started to muck about with NR.

It was easier than some languages I have tried. Sure there were/are problems. Comes with the deal. But I was getting things working.

A few bumps getting the dashboards working, but once I got that going, it was pretty good.
A few bumps with the layout - and they STILL exist. (Chart colours randomly changing colours, sizes of blocks and a couple of other things) But things are going pretty well.

All that considering I don't know any JavaScript, Nominal C++ (Arduino coding helped there) and not much understanding of terms/structures of how all these "modern" languages work.
I DO NOT understand ANY of the examples on the net. It is 70+% un-readable. The rest is guess work.

Sure it is frustrating, but I am trying to learn.

It is no good looking at (over and over) a web page telling me "this is how it is done" if I don't understand what they are saying and can't extract the "syntax" (?) of the command.

I'm doing it to TRY and get better.

But these inconsistencies are not helping because I can't say "That works and I know why/how" with 100% confidence.

I've downloaded 2 40 minute JavaScript tutorials. "In one eye, out the other" kind of.
They are doing things that are not relevant to what I want to do, so..... I don't get how that helps. Maybe for future use?

But also a lot seems to be outside the NR world. So again: how is this useful?

Sorry. But that is me being honest.
And sorry if this seems like a whing. It isn't supposed to be.

I shall look at the two links.
Thanks.

Try the tutorial I linked to, I believe it assumes no prior knowledge. And you need four days, not forty minutes. Unless you get a basic grasp of javascript then, as I said, you are wasting your own time and ours.

That is the second link to which you are referring - right?

I am starting it now and am about 2 pages in.

Colin,

Sorry...... But I've hit a snag with that tutorial already.

I'm not saying it is your fault, but I am wanting to talk to someone to help me understand what it is the problem really is.

Just after adding the JavaScript popup to the index file it goes on to say:

Upon refreshing the page, our browser now displays a friendly greeting ([Figure 5](https://www.learnenough.com/javascript-tutorial/javascript/hello_world/js_web#fig-js_hello_world)).

Then it shows a picture of what it looks like:
(Ok, I can't save and paste the image..... Please have a look)
I get this:

It isn't the same.

I used the exact code on their page:

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

I am already confused because on their picture you see the "Hello world this page includes an alert written in JavaScript" and a pop up.

I ONLY get the popup.

I'm running Ubuntu 18 LTS.
I shall contact the people who own the link, but am mentioning it to show ...... how I am confused.

Read the first line of the info tab of the function node:

A JavaScript function block to run against the messages being received by the node.

Ergo, If you are going to use the function node you better learn Javascript.

Are you sure you are looking at fig 5? It looks the same as yours to me.

Ok.

To clarify:

This is what I see on the example from the link:

This is what I get: