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).
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:
<!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.
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?
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
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.
I suggest sending feedback to the author then. In fact I see the same thing.
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.)
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 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.
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
new Date()
to
dtm.toLocaleTimeString();
Ok, it was hinted at with the line:
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)
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:
command.method (parameters)
Not sure if I should start "another" thread but I ran into this question when chasing another problem:
{"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.
I think I found the answer.
the "_topic":"BedPI" part is accessed by msg._topic.
Silly me.
All good in the end.