bkorda / node-red-contrib-zway

GNU General Public License v3.0
5 stars 4 forks source link

Iteration with for..in results in an extra element #14

Open aweibell opened 1 year ago

aweibell commented 1 year ago

Describe issue crashes with "TypeError: Cannot read property 'title' of undefined" on last +1 element of list

Your flow here

[
    {
        "id": "5a74f90a824af681",
        "type": "zway-output",
        "z": "0dac92a75b2f75fc",
        "name": "",
        "server": "5b24a085ec576130",
        "device": {
            "length": 0,
            "prevObject": {
                "length": 0,
                "prevObject": {
                    "0": {
                        "location": {
                            "ancestorOrigins": {},
                            "href": "http://192.168.4.105:1880/#flow/0dac92a75b2f75fc",
                            "origin": "http://192.168.4.105:1880",
                            "protocol": "http:",
                            "host": "192.168.4.105:1880",
                            "hostname": "192.168.4.105",
                            "port": "1880",
                            "pathname": "/",
                            "search": "",
                            "hash": "#flow/0dac92a75b2f75fc"
                        },
                        "jQuery35103621877289210641": {
                            "events": {
                                "mouseup": [
                                    {
                                        "type": "mouseup",
                                        "origType": "mouseup",
                                        "guid": 3,
                                        "namespace": ""
                                    }
                                ],
                                "click": [
                                    {
                                        "type": "click",
                                        "origType": "click",
                                        "data": null,
                                        "guid": 4246,
                                        "namespace": ""
                                    },
                                    {
                                        "type": "click",
                                        "origType": "click",
                                        "data": null,
                                        "guid": 4259,
                                        "namespace": ""
                                    },
                                    {
                                        "type": "click",
                                        "origType": "click",
                                        "data": null,
                                        "guid": 4499,
                                        "namespace": ""
                                    }
                                ]
                            },
                            "focusin": 3
                        }
                    },
                    "length": 1
                }
            }
        },
        "device_name": "",
        "command": "on",
        "commandType": "zway_cmd",
        "payload": "payload",
        "payloadType": "msg",
        "x": 520,
        "y": 260,
        "wires": []
    },
    {
        "id": "5b24a085ec576130",
        "type": "zway-server",
        "name": "Zway-server",
        "ip": "192.168.4.105",
        "port": "80",
        "authToken": "_____________________________________"
    }
]

Screens / Logs

26 Oct 11:39:01 - [info] [zway-server:Zway-server] discoverDevices: Using cached devices
TypeError: Cannot read property 'title' of undefined
    at /home/pi/.node-red/node_modules/node-red-contrib-zway/nodes/server.js:126:53
    at ServerNode.discoverDevices (/home/pi/.node-red/node_modules/node-red-contrib-zway/nodes/server.js:92:17)
    at ServerNode.getItemsList (/home/pi/.node-red/node_modules/node-red-contrib-zway/nodes/server.js:120:18)
    at /home/pi/.node-red/node_modules/node-red-contrib-zway/zway.js:25:24
    at Layer.handle [as handle_request] (/usr/lib/node_modules/node-red/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/lib/node_modules/node-red/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/usr/lib/node_modules/node-red/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/usr/lib/node_modules/node-red/node_modules/express/lib/router/layer.js:95:5)
    at /usr/lib/node_modules/node-red/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/usr/lib/node_modules/node-red/node_modules/express/lib/router/index.js:346:12)

I have checked the response from ZWay, and it contains a list of devices. They all have a metrics object including a title prop. I added some console.log statements, and it turns out that the for .. in construct seems to add an extra element, where the last element is a function! I also tried replacing with a plain for loop, and then that extra object is not included.

There are diffferent locations in server.js that iterates this way. Below is some code and corresponding log output from another part of the code. First, using the for .. in:

zway-contrib_for-in

And then the plain old for loop:

zway-contrib_for-loop

I also tried a more modern .forEach approach, but then the server.js code seemed to not be executed at all.

Hardware / Software

  - raspberry 4