What data type is an inject node numeric number?

285 views
Skip to first unread message

Alan UK

unread,
Feb 21, 2017, 4:56:34 PM2/21/17
to Node-RED
I'm testing a custom node and wanted to try a negative number so I used an inject node to inject -5 as numeric and the receiving custom node crashed.

I added this debug line: node.log("typeof (msg.payload) is "+typeof(msg.payload))

If I inject "-5" as a string the console states: typeof (msg.payload) is string.

If I inject -5 as a numeric it crashes, presumably at the same line.

It would be nice to handle the situation where a user injects a numeric by converting it to a string so the normal logic works.

So what format/data type **to javascript** is a numeric payload?

Thanks for reading.
Alan

Dave C-J

unread,
Feb 21, 2017, 5:02:15 PM2/21/17
to node...@googlegroups.com
it should be type number
the inject node can inject either a number -5 or string "-5"  - up to you...
Your custom node should handle both (or catch the error correctly)

Alan UK

unread,
Feb 21, 2017, 7:09:55 PM2/21/17
to Node-RED
Dave, that's what I presumed.

I've now flooded the code with debug messages to the console and have found the error: msg.payload.toLowerCase()

So I've moved the checking code to before:

                  // check for numeric payload
                  if (typeof(msg.payload) === "number") {
                    // convert to string otherwise node crashes
                    msg.payload = msg.payload.toString();
                  }

I suppose I ought to check for other types and reject those ........

Thanks.
Alan

Colin Law

unread,
Feb 22, 2017, 2:47:27 AM2/22/17
to node...@googlegroups.com
On 22 February 2017 at 00:09, Alan UK <al...@cooperfamily.me.uk> wrote:
Dave, that's what I presumed.

I've now flooded the code with debug messages to the console and have found the error: msg.payload.toLowerCase()

So I've moved the checking code to before:

                  // check for numeric payload
                  if (typeof(msg.payload) === "number") {


Note that typeof is an operator, not a function, so the correct syntax is
if (typeof msg.payload) === ...)
I don't know what the effect of putting brackets round a variable before operating on it with the typeof operator so whether that is a factor in your problem I have no idea. Someone with a more in-depth knowledge of js would have to comment on that.

Colin
 

Dave C-J

unread,
Feb 22, 2017, 2:56:07 AM2/22/17
to node...@googlegroups.com
If you really need to make everything into a string... We actually have a function for that. 
    RED.util.ensureString() 
Should do the trick.

Alan UK

unread,
Feb 22, 2017, 7:06:52 AM2/22/17
to Node-RED
Thanks Dave & Colin

Dave: that's useful - code now changed.

Colin: Seems both notations are permissible ( typeof xxx or typeof(xxx) ) - see http://www.w3resource.com/javascript/operators/typeof.php. Just happened that when I looked up the syntax I saw the () version first, but why type the () when there is no need to!


Colin Law

unread,
Feb 22, 2017, 7:12:41 AM2/22/17
to node...@googlegroups.com
Oh, you are right. The reference I looked at did not say that. We live and learn.

Thanks

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/0472f2c8-ff38-43ee-85b6-83485dd0fa5c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages