Hi!
Currently trying to wrap my head around node-red and some JS.
Using the code below I'm able to extract data from my payload.RFID and payload.Key however,
if none of these payloads exist they both return as "undefined".
var msg1 = { payload: '{"RFID": "' + msg.payload.RFID+'"}'};
var msg2 = { payload: '{"Key": "' + msg.payload.Key+'"}'};
return [ msg1, msg2 ];
How would you prevent this? I figured I could do it with an If statement, but I'm unable to find a working way to check if "msg.payload.RFID" exists?
bonusquestion:
how do I convert to json directly in the function node? I tried adding a msg3 = JSON.parse(msg1); and then returning msg3 - this didnt work. Currently just using the "json node" to fullfill this.
Attached a picture below showing the outputs, first two messages are with the payloads(correct), two last are when they don't exist(should not output anything).
Edit:
Attaching testing flow.
[{"id":"2f3e2436.82910c","type":"inject","z":"50d2c41c.c2b2fc","name":"All data","topic":"","payload":"{\"ID\": \"52 0B C6 1F\", \"Key\": \"1234\",\"RFID\": \"Mikkel\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":334,"y":167,"wires":[["5a916d21.8495f4"]]},{"id":"1658c707.976409","type":"debug","z":"50d2c41c.c2b2fc","name":"","active":true,"console":"false","complete":"true","x":1159,"y":137,"wires":[]},{"id":"5a916d21.8495f4","type":"function","z":"50d2c41c.c2b2fc","name":"","func":"key = msg.payload.Key\nrfid = msg.payload.RFID\n\n\nvar msg1 = { payload: {RFID: rfid}};\nmsg2 = { payload: {Key: key}};\nreturn [ msg1, msg2 ];","outputs":"2","noerr":0,"x":631,"y":168,"wires":[["1658c707.976409"],["deaa75af.4cbcd8"]]},{"id":"deaa75af.4cbcd8","type":"debug","z":"50d2c41c.c2b2fc","name":"","active":true,"console":"false","complete":"true","x":1136,"y":197,"wires":[]},{"id":"c4e4ea60.eceda8","type":"inject","z":"50d2c41c.c2b2fc","name":"No RFID","topic":"","payload":"{\"ID\": \"52 0B C6 1F\", \"Key\": \"1234\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":326,"y":211,"wires":[["5a916d21.8495f4"]]},{"id":"ee0c9741.31c8f8","type":"inject","z":"50d2c41c.c2b2fc","name":"Neither key nor RFID","topic":"","payload":"{\"ID\": \"52 0B C6 1F\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":288,"y":249,"wires":[["5a916d21.8495f4"]]}]