node-red-node-random how to turn into a json payload

2,429 views
Skip to first unread message

Ben Post

unread,
Aug 26, 2016, 11:40:29 PM8/26/16
to Node-RED
Hi so I have a https://www.npmjs.com/package/node-red-node-random installed, and am using it in a flow to generate a random number (named randnum).

Next is a function node to turn it into a json payload. What's wrong with this:
var new_msg = {};
new_msg.payload = {};
new_msg.payload["Random"] = randnum;
return new_msg;

Cheers

Zenofmud

unread,
Aug 27, 2016, 4:56:04 AM8/27/16
to node...@googlegroups.com
Don’t you need to pass the random number in from the previous node? 
Your function gets the error
ReferenceError: randnum is not defined (line 3, col 29)
for me. 
On Aug 26, 2016, at 11:40 PM, Ben Post <roll...@gmail.com> wrote:

randnum

Zenofmud

unread,
Aug 27, 2016, 5:12:20 AM8/27/16
to node...@googlegroups.com
Shouldn’t the function be

var randnum = msg.payload; 
var new_msg = {};
new_msg.payload = {};
new_msg.payload["Random"] = randnum;
return new_msg;

Dave C-J

unread,
Aug 27, 2016, 6:00:34 AM8/27/16
to node...@googlegroups.com
nodes pass the most used information as msg.payload - the info box on the right should tell you if anything else is available... so the random node puts the number into msg.payload. The name field is there so you can customise the label in the editor window, it doesn't affect the message.

so in the function if you want to move it to another property you then need
    new_msg.payload["Random"] = msg.payload;   // set the "new" payload.Random property to be the "old" payload.



Julian Knight

unread,
Aug 27, 2016, 7:15:25 AM8/27/16
to Node-RED
Of course, in JavaScript, "var[elementname]" is not recommended unless really needed.

Preferred style would be:
var new_msg = {};
new_msg
.payload = { 'Random' : msg.payload };
return new_msg;


or even:
var new_msg = {
 
'payload': {
   
'Random' : msg.payload
  }
};
return new_msg;



Ben Post

unread,
Aug 27, 2016, 9:25:32 PM8/27/16
to Node-RED
awesome thanks

to use node-red I think requires a basic understanding of JavaScript 

Julian Knight

unread,
Aug 28, 2016, 6:36:55 AM8/28/16
to Node-RED
It certainly helps :-) Though there are usually folk around who are happy to help and Google is always a great help. The use of a named subscript is great for those times when you don't know what the element name will be in advance since you can simply put a variable in it. It is also useful when you need to set a name to something that cannot use the simpler dot notation, this can happen for example with MQTT topics which often use "/" - JavaScript allows this in object names but you can't use it in the form "objVar.topic1/topic2", in that case you would have to use "objVar['topic1/topic2']" which works just fine.

You will find though that JavaScript editors with "linting" typically throw a warning when you use named subscripts. JavaScript itself is very forgiving of loose syntax though.
Reply all
Reply to author
Forward
0 new messages