How do I access data from the whole msg. Not just the payload

501 views
Skip to first unread message

dog...@hotmail.co.uk

unread,
Jan 6, 2017, 6:26:48 AM1/6/17
to Node-RED
I am new to node red and I am learning javascript so please bear with me.

I have an ibmiot node posting directly to a debug node set to "complete msg object":

{ "topic": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
  "payload": { "d": { "sensor": 0, "channel": 2, "watts": 2062 } },
  "deviceId": "E3A952088134",
  "deviceType": "xxxxxxxxxxxxxx",
  "commandType": "00:2",
  "format": "json",
  "_msgid": "1be5e29c.e41a1d" }

(I have used "x" to mask sensitive information.)

I write:

    "return msg.deviceId;"

 in a function node to try to expose only "deviceId" and I get:

    "TypeError: Cannot assign to read only property '_msgid' of E3A952088134"

in the debug panel.

It seems to be almost working but I don't understand the nature of the error. How is the assignment involved?
I have spent a few hours searching online but all advice seems to address payload matters.

Could someone enlighten me or point me in the direction of further reading?

Thanks in advance.


Nicholas O'Leary

unread,
Jan 6, 2017, 6:29:55 AM1/6/17
to Node-RED Mailing List
Hi,

Function nodes must return message objects, not individual values.

So the question is, what do you want to do with `msg.deviceId`?

There's a guide to writing Node-RED functions here: http://nodered.org/docs/writing-functions

Nick

--
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/68eafdd1-58fe-4d25-aeae-c95eacaf491f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dog...@hotmail.co.uk

unread,
Jan 6, 2017, 8:20:36 AM1/6/17
to Node-RED
Hi Nick
Thank you for your prompt response.
I am storing this data on a mongodb database which could have potentially hundreds of devices posting to it every few seconds.
To minimise storage space I would like to store only "deviceId" and payload.
To do this I need to somehow repackage "deviceId" with the payload data in a json object.
Any advice would be much appreciated.
Steve

Nicholas O'Leary

unread,
Jan 6, 2017, 8:27:39 AM1/6/17
to Node-RED Mailing List
It will depend slightly on which mongo node you're using.

The node-red-node-mongodb node lets you pick whether it attempts to store the entire message, or just msg.payload.

In this case you have two possible approaches:
 - delete all of the properties on msg you don't care about, or
 - set msg.payload to the object you want to be stored.

For example, the latter can be done with:

    msg.payload = {
        deviceId: msg.deviceId,
        payload: msg.payload
    }
    return msg;

Here, we create a new payload object that has just the two properties you want pulled out of the message.

Nick

--
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.

Julian Knight

unread,
Jan 6, 2017, 8:28:24 AM1/6/17
to Node-RED
You need something like:

var msg1 = {};
msg
.payload = { "deviceid": msg.deviceid, "payload": msg.payload };
return msg1;

All the output to pass to MongoDB should be in the output payload if you don't want the other stuff. You may need to add other data to msg1 of course depending on how you are writing to the db

Nicholas O'Leary

unread,
Jan 6, 2017, 8:30:22 AM1/6/17
to Node-RED Mailing List
Julian - small typo in your solution - you set msg.payload, not msg1.payload.

That said, in general, we recommend you reuse the msg object passed in rather than create a new object if all you're doing is setting a property of it.

Nick

--
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.

Julian Knight

unread,
Jan 6, 2017, 8:33:38 AM1/6/17
to Node-RED
Oops! Thanks. Shouldn't really do this while I'm working :-}

I created a new object because changing the msg.payload from the msg.payload can cause issues if you get the order wrong, simpler not too. Though not as efficient in memory use of course.
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.

dog...@hotmail.co.uk

unread,
Jan 6, 2017, 8:42:55 AM1/6/17
to Node-RED
Thanks Nick
Your example gives me just the information I was looking for.


On Friday, 6 January 2017 11:26:48 UTC, dog...@hotmail.co.uk wrote:
Reply all
Reply to author
Forward
0 new messages