How to create a function to access JSON index in node.js?

1,314 views
Skip to first unread message

Francisco Cuesta

unread,
Apr 28, 2016, 9:56:02 AM4/28/16
to Node-RED
Hello all,

I am working with node-red and I would like to create my custom function to access some index from the incoming message. The incoming message looks like this

    { "node": "02010101", "base64": "Cro=" }



It comes out from the json function block in node-red (*A function that parses the msg.payload to convert a JSON string to/from a javascript object. Places the result back into the payload*), I can use the debug block to obtain the index *base64*, however if I try to do the same with my own function to proces that later, I cannot and I get the error

    TypeError: Cannot assign to read only property '_msgid' of Cro=



My function is really silly for now it is just

    return msg["base64"];



I understand that it complains that there is no property in the incoming message, so I would like to access to hat index, how can I do it?

thanks in advnce,

regards

EDIT: if I set the debug block to show the whole message object not just the msg.base64 itself, I get this

    { "node": "02010101", "base64": "Cro=", "_msgid": "6babd6e.f945428" }




Nicholas O'Leary

unread,
Apr 28, 2016, 9:59:04 AM4/28/16
to Node-RED Mailing List
Hi,

Ben already answered this question on Stack Overflow for you.

You should not return just msg["base64"] - you need to return a message object that has a payload property containing the value you want.

msg.payload = msg.base64;
return msg;


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

Ben Hardill

unread,
Apr 28, 2016, 10:00:35 AM4/28/16
to Node-RED
Assuming the inject node is passing in a string and the next node is doing a string to JSON transform (You don't need this with the latest version of Node-RED as it can inject JSON directly)

What do the next 2 nodes do (The ones before the function node)? Are they custom?

Francisco Cuesta

unread,
Apr 28, 2016, 10:04:45 AM4/28/16
to Node-RED
Yes the input in inject is a string

The function getting decrypted field  does
var decrypted = msg.decrypted;
return decrypted;

and function b64Decode
var data = msg.base64;
return data

This last one will decode the field base64 from base64 to string or hex.

thanks

Francisco Cuesta

unread,
Apr 28, 2016, 10:05:58 AM4/28/16
to Node-RED
As i told him, it keeps getting me an error

Nicholas O'Leary

unread,
Apr 28, 2016, 10:06:30 AM4/28/16
to Node-RED Mailing List
The function getting decrypted field  does 
var decrypted = msg.decrypted;
return decrypted;

and function b64Decode
var data = msg.base64;
return data


Once again, you must not return single values like this. You must return message objects.

msg.payload = msg.decrypted;
return msg;

msg.payload = msg.base64;
return msg;

--

Nicholas O'Leary

unread,
Apr 28, 2016, 10:07:51 AM4/28/16
to Node-RED Mailing List
Did you fix it in all your functions or just that one? Your latest email indicates you have multiple functions that are returning simple properties and not message objects.

Nick

Francisco Cuesta

unread,
Apr 28, 2016, 10:09:45 AM4/28/16
to Node-RED
A link of my clipboard

[{"id":"77562d5e.8867e4","type":"inject","z":"6b578bc1.5cd8ec","name":"","topic":"","payload":"{\"gatewayEui\":\"0000024B080E0139\",\"nodeEui\":\"02010101\",\"time\":\"2016-04-28T10:15:17.752456663Z\",\"frequency\":868.1,\"dataRate\":\"SF12BW125\",\"rssi\":-82,\"snr\":7.2,\"rawData\":\"gAEBAQIAQQEDkEC5IjDH\",\"data\":\"Cro=\"}","payloadType":"str","repeat":"","crontab":"","once":false,"x":249,"y":178,"wires":[["c77cc83d.65a46","cb5b4299.e27cb"]]},{"id":"cb52e0af.88d1f","type":"decrypt-ttn","z":"6b578bc1.5cd8ec","name":"","key":"2B7E151628AED2A6ABF7158809CF4F3C","x":449,"y":292,"wires":[["f3687d16.cd2998","73c1414a.a9751"]]},{"id":"c77cc83d.65a46","type":"json","z":"6b578bc1.5cd8ec","name":"","x":395,"y":198,"wires":[["cb52e0af.88d1f","a11a8ef4.51ffd"]]},{"id":"f3687d16.cd2998","type":"debug","z":"6b578bc1.5cd8ec","name":"payload?","active":true,"console":"true","complete":"true","x":690,"y":279.5,"wires":[]},{"id":"cb5b4299.e27cb","type":"debug","z":"6b578bc1.5cd8ec","name":"direct","active":false,"console":"true","complete":"payload","x":562,"y":139.5,"wires":[]},{"id":"a11a8ef4.51ffd","type":"debug","z":"6b578bc1.5cd8ec","name":"formatted","active":false,"console":"true","complete":"payload","x":590,"y":209.5,"wires":[]},{"id":"e5393aa8.73539","type":"debug","z":"6b578bc1.5cd8ec","name":"decrypted","active":true,"console":"true","complete":"decrypted","x":777,"y":365.5,"wires":[]},{"id":"faaf724.b4af59","type":"debug","z":"6b578bc1.5cd8ec","name":"mote Payload","active":true,"console":"true","complete":"base64","x":776,"y":467.5,"wires":[]},{"id":"107d9732.1b0499","type":"function","z":"6b578bc1.5cd8ec","name":"getting decrypted field ","func":"var decrypted = msg.decrypted;\nreturn decrypted;\n\n","outputs":1,"noerr":0,"x":434,"y":446,"wires":[["e5393aa8.73539","96fd58a9.f71608"]]},{"id":"72afcb60.6befbc","type":"function","z":"6b578bc1.5cd8ec","name":"b64Decode","func":"var data = msg.base64;\nreturn data","outputs":1,"noerr":0,"x":630,"y":602,"wires":[["3411b3ca.bc712c"]]},{"id":"3411b3ca.bc712c","type":"debug","z":"6b578bc1.5cd8ec","name":"ee","active":true,"console":"true","complete":"base64","x":828,"y":603.5,"wires":[]},{"id":"96fd58a9.f71608","type":"json","z":"6b578bc1.5cd8ec","name":"parsing JSON","x":597,"y":520.5,"wires":[["faaf724.b4af59","72afcb60.6befbc"]]},{"id":"73c1414a.a9751","type":"json","z":"6b578bc1.5cd8ec","name":"parsing JSON","x":338,"y":375,"wires":[["107d9732.1b0499"]]}]

Nicholas O'Leary

unread,
Apr 28, 2016, 10:11:28 AM4/28/16
to Node-RED Mailing List
I don't have the decrypt-ttn node - where is that from?

You need to fix both of your Functions as I've said a few times. You may still be hitting another issue, but those Function nodes definitely need fixing.

Nick

On 28 April 2016 at 15:09, Francisco Cuesta <ndar...@gmail.com> wrote:
A link of my clipboard

[{"id":"77562d5e.8867e4","type":"inject","z":"6b578bc1.5cd8ec","name":"","topic":"","payload":"{\"gatewayEui\":\"0000024B080E0139\",\"nodeEui\":\"02010101\",\"time\":\"2016-04-28T10:15:17.752456663Z\",\"frequency\":868.1,\"dataRate\":\"SF12BW125\",\"rssi\":-82,\"snr\":7.2,\"rawData\":\"gAEBAQIAQQEDkEC5IjDH\",\"data\":\"Cro=\"}","payloadType":"str","repeat":"","crontab":"","once":false,"x":249,"y":178,"wires":[["c77cc83d.65a46","cb5b4299.e27cb"]]},{"id":"cb52e0af.88d1f","type":"decrypt-ttn","z":"6b578bc1.5cd8ec","name":"","key":"2B7E151628AED2A6ABF7158809CF4F3C","x":449,"y":292,"wires":[["f3687d16.cd2998","73c1414a.a9751"]]},{"id":"c77cc83d.65a46","type":"json","z":"6b578bc1.5cd8ec","name":"","x":395,"y":198,"wires":[["cb52e0af.88d1f","a11a8ef4.51ffd"]]},{"id":"f3687d16.cd2998","type":"debug","z":"6b578bc1.5cd8ec","name":"payload?","active":true,"console":"true","complete":"true","x":690,"y":279.5,"wires":[]},{"id":"cb5b4299.e27cb","type":"debug","z":"6b578bc1.5cd8ec","name":"direct","active":false,"console":"true","complete":"payload","x":562,"y":139.5,"wires":[]},{"id":"a11a8ef4.51ffd","type":"debug","z":"6b578bc1.5cd8ec","name":"formatted","active":false,"console":"true","complete":"payload","x":590,"y":209.5,"wires":[]},{"id":"e5393aa8.73539","type":"debug","z":"6b578bc1.5cd8ec","name":"decrypted","active":true,"console":"true","complete":"decrypted","x":777,"y":365.5,"wires":[]},{"id":"faaf724.b4af59","type":"debug","z":"6b578bc1.5cd8ec","name":"mote Payload","active":true,"console":"true","complete":"base64","x":776,"y":467.5,"wires":[]},{"id":"107d9732.1b0499","type":"function","z":"6b578bc1.5cd8ec","name":"getting decrypted field ","func":"var decrypted = msg.decrypted;\nreturn decrypted;\n\n","outputs":1,"noerr":0,"x":434,"y":446,"wires":[["e5393aa8.73539","96fd58a9.f71608"]]},{"id":"72afcb60.6befbc","type":"function","z":"6b578bc1.5cd8ec","name":"b64Decode","func":"var data = msg.base64;\nreturn data","outputs":1,"noerr":0,"x":630,"y":602,"wires":[["3411b3ca.bc712c"]]},{"id":"3411b3ca.bc712c","type":"debug","z":"6b578bc1.5cd8ec","name":"ee","active":true,"console":"true","complete":"base64","x":828,"y":603.5,"wires":[]},{"id":"96fd58a9.f71608","type":"json","z":"6b578bc1.5cd8ec","name":"parsing JSON","x":597,"y":520.5,"wires":[["faaf724.b4af59","72afcb60.6befbc"]]},{"id":"73c1414a.a9751","type":"json","z":"6b578bc1.5cd8ec","name":"parsing JSON","x":338,"y":375,"wires":[["107d9732.1b0499"]]}]

--

Francisco Cuesta

unread,
Apr 28, 2016, 10:15:00 AM4/28/16
to Node-RED
That module  is coming from here

My other function does not give any specific value, ir returns the whole decrypt key for the next block only the last one, b64Decode should retreive a key value that would be base 64 decoded.

thanks!

Francisco Cuesta

unread,
Apr 28, 2016, 10:15:05 AM4/28/16
to Node-RED

Francisco Cuesta

unread,
Apr 28, 2016, 10:47:32 AM4/28/16
to Node-RED
Thanks Nick,

you wer right, I messed with the functions, and I overlook your message... Now it is working fine

Thanks again!
Reply all
Reply to author
Forward
0 new messages