TypeError: Cannot read property '0' of undefined
Closed this issue · 6 comments
I am testing the module with my RXV775. At first it works as expected, but after several hours deployed the flow, node red start crashing when the a/v receiver sends any event. Still sends the "last" command triggered by the flow, but crashes when the receiver sends the response event.
2 Dec 12:22:21 - [red] Uncaught Exception:
2 Dec 12:22:21 - TypeError: Cannot read property '0' of undefined
at /data/node_modules/node-red-contrib-avr-yamaha/avr-yamaha.js:371:55
at Parser. (/data/node_modules/xml2js/lib/parser.js:303:18)
at emitOne (events.js:96:13)
at Parser.emit (events.js:188:7)
at Object.onclosetag (/data/node_modules/xml2js/lib/parser.js:261:26)
at emit (/data/node_modules/sax/lib/sax.js:624:35)
at emitNode (/data/node_modules/sax/lib/sax.js:629:5)
at closeTag (/data/node_modules/sax/lib/sax.js:889:7)
at Object.write (/data/node_modules/sax/lib/sax.js:1436:13)
at Parser.exports.Parser.Parser.parseString (/data/node_modules/xml2js/lib/parser.js:322:31)
at Parser.parseString (/data/node_modules/xml2js/lib/parser.js:5:59)
at exports.parseString (/data/node_modules/xml2js/lib/parser.js:354:19)
Removing the AVR-YAMAHA-IN node and deploying it again without any configuration change, only selecting the proper avr already configured, solves the problem until it start crashing again.
Thanks for the report. Seems like the incoming event data has a format, that I do not expect. I've updated the nodes and tried to catch the error. Please have a look at version 0.8.1.
I also added a warning. If you enable debug mode in the config node, then the unexpected event data is logged in the node-red debug output.
Would be happy if you could post the data here, as I would be curious what data arrives.
Cannot reproduce your error here with my AVR.
Thanks for the support. I tried the new version and after a while it started failing again. I modified the line #335 to print the received event:
3 Dec 00:48:15 - [info] [avr-yamaha:yamaha receiver] STARTING event listener
3 Dec 00:48:15 - [info] [avr-yamaha:yamaha receiver] UDP client listening on 0.0.0.0:1900
3 Dec 00:48:15 - [info] [mqtt-broker:b2e78784.11b098] Connected to broker: mqtt://192.168.1.200:1883
3 Dec 00:48:25 - [info] [avr-yamaha:yamaha receiver] sending PUT command:<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>On</Power></Power_Control></Zone_2></YAMAHA_AV>
3 Dec 00:48:26 - [info] [avr-yamaha:yamaha receiver] UPnP Event from [192.168.1.202] --> NOTIFY * HTTP/1.1
Host: 239.255.255.250:1900
NT: urn:schemas-yamaha-com:service:X_YamahaRemoteControl:1
NTS: yamaha:propchange
USN: uuid:::urn:schemas-yamaha-com:service:X_YamahaRemoteControl:1
<?xml version="1.0" encoding="utf-8"?><YAMAHA_AV cmd="EVENT"><Zone_2><Property>Power</Property><Property>Play_Info</Property></Zone_2></YAMAHA_AV>
3 Dec 00:48:26 - [red] Uncaught Exception:
3 Dec 00:48:26 - TypeError: Cannot read property '0' of undefined
at /data/node_modules/node-red-contrib-avr-yamaha/avr-yamaha.js:382:37
at Parser.<anonymous> (/data/node_modules/xml2js/lib/parser.js:303:18)
at emitOne (events.js:96:13)
at Parser.emit (events.js:188:7)
at Object.onclosetag (/data/node_modules/xml2js/lib/parser.js:261:26)
at emit (/data/node_modules/sax/lib/sax.js:624:35)
at emitNode (/data/node_modules/sax/lib/sax.js:629:5)
at closeTag (/data/node_modules/sax/lib/sax.js:889:7)
at Object.write (/data/node_modules/sax/lib/sax.js:1436:13)
at Parser.exports.Parser.Parser.parseString (/data/node_modules/xml2js/lib/parser.js:322:31)
at Parser.parseString (/data/node_modules/xml2js/lib/parser.js:5:59)
at exports.parseString (/data/node_modules/xml2js/lib/parser.js:354:19)
at Socket.<anonymous> (/data/node_modules/node-red-contrib-avr-yamaha/avr-yamaha.js:365:13)
at emitTwo (events.js:106:13)
at Socket.emit (events.js:191:7)
at UDP.onMessage (dgram.js:548:8)
After several tests, the node works as expected if you are working with the main zone. If you want to control another zone, it crashes when the first response event is received:
{"YAMAHA_AV":{"$":{"cmd":"EVENT"},"Zone_2":[{"Property":["Power","Play_Info"]}]}}
I think the problem is line #374 looking for Main_Zone in result... it is in a try, it fails, mainZone is set to "undefined" and this is the reason why it crashes in #382.
A dirt way to avoid this is to check if mainZone is undefined after the try and try again with the next zone...
if (mainZone == undefined){ try{ mainZone = result.YAMAHA_AV.Zone_2; }catch (e){ if (node.debug) { node.warn('Unknown event message format: ' + JSON.stringify(result)); } return; }
Thanks for the help. Your're right. It crashed when events other than from the Main_Zone were received. Good point. I tried to fix it in a way, that also the event data of the correct zone is ouput. Should also work for zone 3 + 4 now.
Please have a look at v0.8.2.
Thank you, every flow I have are working without crashes now.
Thanks for the feedback.