Extract value from an object

6,382 views
Skip to first unread message

Paul Reed

unread,
Oct 4, 2014, 4:56:47 PM10/4/14
to node...@googlegroups.com
I'm playing with Lawrence's mobiUI, and struggling trying to extract the value (is it a json object?) from the dispatcher.
For example, the output from the dispatcher in the debug is;

(Object) {"id": "sld-1", "value": "19.3"}

I just want to retrieve the 19.3 as a msg.payload, so that it can be re-used.

Thanks. Paul




Nicholas O'Leary

unread,
Oct 4, 2014, 4:59:05 PM10/4/14
to node...@googlegroups.com
Hi Paul,

you can use a Change node like this:

[{"id":"51ff7d89.ae0084","type":"change","action":"replace","property":"payload","from":"","to":"msg.payload.value","reg":false,"name":"","x":427,"y":379,"z":"85252611.7adad8","wires":[[]]}]

Or a function node:

    msg.payload = msg.payload.value;
    return msg;

Nick

--
http://nodered.org
---
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.
For more options, visit https://groups.google.com/d/optout.

Paul Reed

unread,
Oct 4, 2014, 5:17:39 PM10/4/14
to node...@googlegroups.com
With both, I get;
'TypeError: cannot read property 'value' of undefined'

Nicholas O'Leary

unread,
Oct 4, 2014, 5:19:40 PM10/4/14
to node...@googlegroups.com
Paul,

is you debug node set to display the complete message object or just payload?

From that error, I'm guessing it is the former - I had assumed the latter in my original reply.

Try either of these:

[{"id":"51ff7d89.ae0084","type":"change","action":"replace","property":"payload","from":"","to":"msg.value","reg":false,"name":"","x":427,"y":379,"z":"85252611.7adad8","wires":[[]]}]

Function:
    msg.payload = msg.value;
    return msg;

On 4 October 2014 22:17, Paul Reed <paul...@gmail.com> wrote:
With both, I get;
'TypeError: cannot read property 'value' of undefined'

--

Paul Reed

unread,
Oct 4, 2014, 5:26:27 PM10/4/14
to node...@googlegroups.com
Yes, you are correct - complete message.
Your latest works and does exactly what I want, Thanks.

Paul

Dave C-J

unread,
Oct 4, 2014, 6:19:45 PM10/4/14
to node...@googlegroups.com

We should maybe make the tiny (topic) text in debug say "complete message"... when in that mode to help ?

Nicholas O'Leary

unread,
Oct 4, 2014, 6:24:15 PM10/4/14
to node...@googlegroups.com
Dave,


On 4 October 2014 23:19, Dave C-J <dce...@gmail.com> wrote:

We should maybe make the tiny (topic) text in debug say "complete message"... when in that mode to help ?

--

Dave C-J

unread,
Oct 5, 2014, 3:48:23 AM10/5/14
to node...@googlegroups.com

Great !
(And the other potential debug pr (reverse highlighting) looks good too.)

Lawrence Griffiths

unread,
Oct 5, 2014, 4:06:50 PM10/5/14
to node...@googlegroups.com
Paul are you modifying the dispatcher javascript or using one it's outputs?

in dispatcher JavaScript
you can access both id and value 
obj.v

On the outbound msg to another node what is passed is
msg.value 

so changing the slider on UI should give this in degbug window (Object) { "id": "sld-1", "value": "43" } 
I should probably use payload

Lawrence

Paul Reed

unread,
Oct 5, 2014, 4:27:36 PM10/5/14
to node...@googlegroups.com
I was using the dispatcher's outputs, but I see what you mean, and by changing the javascrip to;

// slider  sld-1 - output 3
} else if(obj.id=="sld-1"){
msg.payload = obj.v;
return [null, null, msg, null];

I can get the value directly as an output, which makes things simpler (for me!!)

Paul


Lawrence Griffiths

unread,
Oct 6, 2014, 4:07:51 AM10/6/14
to node...@googlegroups.com
Yep will update the init version I'm working on to use msg.payload

Paul Reed

unread,
Oct 8, 2014, 2:40:57 PM10/8/14
to node...@googlegroups.com
Ha, I see that you've updated it to msg.playload!!!

Paul

Lawrence Griffiths

unread,
Oct 8, 2014, 3:00:08 PM10/8/14
to node...@googlegroups.com
Yep but both ID & V are passed in payload.
So you may want to delete ID for nodes that just expect a value.

I've also created a version with secure user authentication.
I'm putting together a short blog on how to set it up, should have that done in the next week.
 
Lawrence

Paul Reed

unread,
Oct 8, 2014, 3:25:54 PM10/8/14
to node...@googlegroups.com
You have 'playload' not 'payload' in the ui dispatcher (4 times!).

Paul

Lawrence Griffiths

unread,
Oct 8, 2014, 3:35:30 PM10/8/14
to node...@googlegroups.com
Arr will change thanks not enough time to test....
Reply all
Reply to author
Forward
0 new messages