mysl node msg.payload from array to number?

1,170 views
Skip to first unread message

Lestat De la Morte

unread,
Jan 26, 2017, 12:29:38 AM1/26/17
to Node-RED
Hi, is there a way to get rid of the brackets and the curly braces when retreiving a value from the mysql node? I would like to send this value to the dashboard as a number.

Thanks.

Mark Setrem

unread,
Jan 26, 2017, 4:44:34 AM1/26/17
to Node-RED
Yes, you can use several different nodes to do it.

A quick google for "javascript arrays and objects" should give you a list of pages that will show you how to work with arrays and objects and then you can use a function node.

If you post an example it might be easier to explain.

Lestat De la Morte

unread,
Jan 30, 2017, 3:19:49 PM1/30/17
to Node-RED
Thanks Mark, 

forr example, i have this DB named Poste12 where there's a column named duration with time  values  and another named id number   and this query: 


msg.topic = "SELECT duration FROM `Poste12` WHERE `id` = 201";
return msg;

return:

SELECT duration FROM `Poste12` WHERE `id` = 201 : msg.payload : array [1][ { "duration": "00:00:17" }

So i would like to have only the value: 000:00:17 without the rest...

Is it possible?

Thanks.

Mark Setrem

unread,
Jan 30, 2017, 3:39:06 PM1/30/17
to Node-RED
Well I doubt you copied the whole message there as it should end with a ]

So assuming that msg.payload = [ { "duration": "00:00:17" }] 

msg.payload[0]duration = "00:00:17" 

But being able to manipulate javascript objects and arrays is fundamental to understanding node-red so you need to read some of the online guides such as : http://www.w3schools.com/js/js_arrays.asp

Colin Law

unread,
Jan 30, 2017, 4:59:23 PM1/30/17
to node...@googlegroups.com
On 30 January 2017 at 20:39, Mark Setrem <mse...@gmail.com> wrote:
> Well I doubt you copied the whole message there as it should end with a ]
>
> So assuming that msg.payload = [ { "duration": "00:00:17" }]
>
> msg.payload[0]duration = "00:00:17"

Did you mean
msg.payload = msg.payload[0].duration;

Colin

Lestat De la Morte

unread,
Jan 30, 2017, 6:05:05 PM1/30/17
to Node-RED
Awsome, it works! 

Yes Mark, i forgot a closing bracket at the end, and yes, i will learn Javascript as i'm learning  programming in general. It's all new for me and FUN. Thanks for the link and your assistance. i'm reading everything i can and you guys do a wonderfull job here.

Indeed Collin there has to be a dot after [0]


Le jeudi 26 janvier 2017 00:29:38 UTC-5, Lestat De la Morte a écrit :

Lestat De la Morte

unread,
Jan 30, 2017, 9:49:50 PM1/30/17
to Node-RED
Argh! i've red the docs on arrays and objects but i really have to start from the very begining in javascript, meanwhile here's this one:

payload is:

 [ { "SEC_TO_TIME (SUM(TIME_TO_SEC(duration)))": "01:27:01" } ]

If i input:
msg.payload = msg.payload[0].SEC_TO_TIME(SUM(TIME_TO_SEC(duration)));

It gives:

ReferenceError: SUM is not defined (line 1, col 43)

And if i try with [1] it gives:

TypeError: Cannot read property 'SEC_TO_TIME' of undefined

Maybe it doesn't work when there is a function?

Back to read.




Le jeudi 26 janvier 2017 00:29:38 UTC-5, Lestat De la Morte a écrit :

Colin Law

unread,
Jan 31, 2017, 3:31:17 AM1/31/17
to node...@googlegroups.com
For that you will need to use the alternative attribute access technique
msg.payload = msg.payload[0]["SEC_TO_TIME(SUM(TIME_TO_SEC(duration)))"];

Note there is no dot in this case as you are accessing the object
using array-like syntax to get the key value.

Colin

On 31 January 2017 at 02:49, Lestat De la Morte
> --
> 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+u...@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/e5db7f2b-9f40-4280-99ee-909ad00e38af%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Mark Setrem

unread,
Jan 31, 2017, 3:40:11 AM1/31/17
to Node-RED
Yep should have been a dot there, but bizarrely the change node doesn't require it

steve rickus

unread,
Jan 31, 2017, 9:11:12 AM1/31/17
to Node-RED
A more readable alternative would be to use an alias in your query, so you don't end up with such complicated object properties. You did not mention which type of database you are querying, but in SQL (for instance) I would change the query to be something like:

SELECT Id, SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) AS Duration FROM ...

and then in node-red I can reference it much more simply as msg.payload[0].Duration or msg.payload[0]["Duration"] -- note that the first syntax can only be used with property names that do not include whitespace or punctuation.

Incidentally, most database queries will return an array of results, even if the result is only a singe number like in the original question. This is why there appears to be extra brackets in the payload. So getting the first result requires the msg.payload[0] syntax.
--
Steve

Mario Bianchi

unread,
Feb 1, 2017, 10:09:30 AM2/1/17
to Node-RED
It is a bit tricky, but you can try accessing this way:

a[0][Object.keys(a[0])]

The first pair of brackets choose the array element like you are currently doing, the 2nd pair will go  get the name of the attribute and use that name to select the right attribute.

Do not like much this solution, but it works.

Regards,
Mario.


Lestat De la Morte

unread,
Feb 1, 2017, 3:23:43 PM2/1/17
to Node-RED
Thank you guys, all three solutions are working, it shows my value in the dashboard  but there is a function error in the debug : TypeError: Cannot read property 'whatever is after msg.paload[0]' of undefined . but if i send a message from the inject node  directly to the mylql query node it doesn't. Can i live with that? can it harm anything?

steve rickus

unread,
Feb 2, 2017, 9:22:06 AM2/2/17
to Node-RED
At this point, it would be helpful for us if you could export your flow and paste it here for us to see. Otherwise, we are just guessing...
Reply all
Reply to author
Forward
0 new messages