Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught Exception: Cannot read properties of undefined (reading 'length') #302

Closed
unclej84 opened this issue Jan 27, 2022 · 19 comments · Fixed by #319
Closed

Uncaught Exception: Cannot read properties of undefined (reading 'length') #302

unclej84 opened this issue Jan 27, 2022 · 19 comments · Fixed by #319
Labels

Comments

@unclej84
Copy link

Describe the bug
Updated NodeRED to 2.2.0 (NodeJS 16) today and no I constantly get Uncaught exception errors on starting up

Expected behavior
No crash on startup

Log
TypeError: Cannot read properties of undefined (reading 'length') at new HueBridgeMessage (/data/node_modules/node-red-contrib-huemagic/huemagic/utils/messages.js:17:61) at HueBridge.get (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:306:23) at /data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge.js:84:30 at EventEmitter.<anonymous> (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:556:7) at EventEmitter.emit (node:events:390:28) at EventEmitter.emit (node:domain:475:12) at HueBridge.pushUpdatedState (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:275:16) at Timeout._onTimeout (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:177:13) at listOnTimeout (node:internal/timers:557:17) at processTimers (node:internal/timers:500:7)

Please complete the following information:

  • Node version: 4.1.0
  • Node-RED version: 2.2.0
  • NodeJS version: 16.13.1
  • Device running Node-RED: Docker (20.10.12) on RaspberryPi 4B+ (4GB)

Additional context
Problem occured after upgrade to Node-RED 2.2.0 but even reverting image back to 2.1.6 does not work. Before it worked with 4,1.0 on 2.1.6

@unclej84 unclej84 added the bug label Jan 27, 2022
@unclej84
Copy link
Author

The responsible line in messages.js seems to be
this.message.payload.starterKitId = resource.starterkitid.length > 0 ? resource.starterkitid : false;.
The api returns "starterkitid": "", in the config section of the json.
I have then tried to set it to false by replacing the line with this.message.payload.starterKitId = false; but got another error.

TypeError: Cannot convert undefined or null to object at Function.entries (<anonymous>) at new HueBridgeMessage (/data/node_modules/node-red-contrib-huemagic/huemagic/utils/messages.js:45:39) at HueBridge.get (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:306:23) at /data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge.js:84:30 at EventEmitter.<anonymous> (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:556:7) at EventEmitter.emit (node:events:390:28) at EventEmitter.emit (node:domain:475:12) at HueBridge.pushUpdatedState (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:275:16) at Timeout._onTimeout (/data/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:177:13) at listOnTimeout (node:internal/timers:557:17) at processTimers (node:internal/timers:500:7)

Is seems that the lookup of whitelist does also not work - the api returns this directly after starterkitid. These are the last two entries of the config section.

The fields touchlink, autoupdate and updated are not included in the config section. Api-Version is shown as 1.48.0.

If you need further information or details, please let me know.

@rotor5
Copy link

rotor5 commented Jan 30, 2022

Hello,
I have the same Issue and would be happy if there is any solution for this.

@mkloss33
Copy link

Hi all, same here for me. NodeRed crashing since a couple of days with the very same error message.

TypeError: Cannot read properties of undefined (reading 'length') at new HueBridgeMessage (/home/pi/.node-red/node_modules/node-red-contrib-huemagic/huemagic/utils/messages.js:17:61) at HueBridge.get (/home/pi/.node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:306:23) at /home/pi/.node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge.js:84:30 at EventEmitter.<anonymous> (/home/pi/.node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:556:7) at EventEmitter.emit (node:events:390:28) at HueBridge.pushUpdatedState (/home/pi/.node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:275:16) at Timeout._onTimeout (/home/pi/.node-red/node_modules/node-red-contrib-huemagic/huemagic/hue-bridge-config.js:177:13) at listOnTimeout (node:internal/timers:557:17) at processTimers (node:internal/timers:500:7)

@unclej84
Copy link
Author

Hello,

not sure when Foddy will have time to investigate / fix this so maybe my temporary solution also helps you.
I checked the API output manually via: https://<bridge-ip>/api/<api-key> for the values of starterkitid and whitelist.
Then I checked messages.js in huemagic/utils/-folder and adjusted it manually here (in my case the string was empty so I just put = false;) and here (Just if you need the user listing function - otherwise leave it and just comment the for loop). Finally I commented this lines out (the whole For-loop).
After that it is working for now for me. Hope this helps you for now too.

Regards Jörg

@jeroenhe
Copy link

I tried upgrading from 3.0.0 and it looks like I have the same exception. I've reverted back to 3.0.0 which (obviously?) fixed the issue. Thank you for posting the work-around, I might try that later.

@unclej84
Copy link
Author

@jeroenhendricksen please note that the migration to 4.X.X also requires some additional adjustment to the nodes as stated here because of the UUIDs in the API.

@Rayhzvj
Copy link

Rayhzvj commented Feb 5, 2022

Currently facing the same issues and this prevent NR from starting.

@Swiftnesses
Copy link

@unclej84 this got me up and running again - thank you.

@memoir
Copy link

memoir commented Feb 9, 2022

Same issue on a fresh installation, not upgrading from a previous version.

@ptweety
Copy link
Contributor

ptweety commented Feb 12, 2022

Hello,

not sure when Foddy will have time to investigate / fix this so maybe my temporary solution also helps you. I checked the API output manually via: https://<bridge-ip>/api/<api-key> for the values of starterkitid and whitelist. Then I checked messages.js in huemagic/utils/-folder and adjusted it manually here (in my case the string was empty so I just put = false;) and here (Just if you need the user listing function - otherwise leave it and just comment the for loop). Finally I commented this lines out (the whole For-loop). After that it is working for now for me. Hope this helps you for now too.

Regards Jörg

Hi @unclej84 ,

wouldn't it be better to change the respective lines like this:

this.message.payload.starterKitId = resource.starterkitid && resource.starterkitid.length > 0 ? resource.starterkitid : false;

and

		// GET USERS
		if (resource["whitelist"]) {
			for (const [userID, user] of Object.entries(resource["whitelist"]))
			{
				this.message.payload.users.push({
					user: userID,
					name: user["name"],
					created: user["create date"],
					lastAccess: user["last use date"]
				});
			}	
		}

@unclej84
Copy link
Author

Hi @ptweety,

I am not so deep in JS to know if the evaluation already stops when the first &&-part stops or if he is still trying to evaluate the second part which throws the error. So, I have to rely on your knowledge here. The second part of course looks good.

But anyway, the question is why it does not get the information when I see them in the API.

For error handling your solution is good but still the - let's say underlying issue - should be investigated and hopefully solved.

@ptweety
Copy link
Contributor

ptweety commented Feb 12, 2022

Well, the issue is simply that the structure of the incoming data (i.e. the resource object) is not guaranteed. e.g.:

resource = {
  bridge_id: "001788fffe40cd07",
  id: "40b4b12b-330f-420e-9e39-78765225f1a1",
  id_v1: "",
  time_zone: {
    time_zone: "Europe/Berlin",
  },
  type: "bridge",
  updated: "2022-02-12T14:38:48+01:00",
  types: [
    "bridge",
  ],
}

You can see, that there is no starterkitid or whitelist. If you then access children of these elements without checking the existence of the element itself you get the exception.

in Line 34 there is a proper check already in the code:

this.message.payload.touchlinkEnabled = (resource["touchlink"] && resource["touchlink"] == true) ? true : false;

@Rollem1
Copy link

Rollem1 commented Feb 13, 2022

Hi @unclej84, @ptweety,

Thanks unclej84 for pointing me to this thread - I had opened another topic but on reflection this is the same issue I had. Just to confirm I tried both of your fixes and they both work for me.

Cheers!
Jon

@dmoibm
Copy link

dmoibm commented Feb 14, 2022

Hi @ptweety

Thanks! Your solution works fine!

@cmuench
Copy link

cmuench commented Mar 6, 2022

Any chance that this can be merged?
I patched my local node-RED and can also confirm that the PR fixes the issue on my machine.

@isaac-the-newt
Copy link

Hi all,

I've found that disabling the HUE BRIDGE node still allows HUE devices to function with Node-Red without the continual error trapping and restarting of Node-Red described by others in this thread. I had raised this issue with Foddy some time ago but as it has gone unresolved, I had to find a work around.
HUE disabled

@lougreenwood
Copy link

Thanks for the tip @isaac-the-newt, but it doesn't help when this error stops Node Red from being able to start.

Foddy added a commit that referenced this issue Mar 20, 2022
* Commands are now re-executed up to three times if they fail due to a bridge timeout
* The "image" option on the "Hue Light" node will now set the corresponding gradient colors on supported resources
* Better handling of broken connections to the bridge ([#309](#309)) (thx)
* Fixed an error with the "Hue Scenes" node on newer bridge firmwares ([#335](#335)) ([#339](#339)) (thx)
* Fixed an error with uncaught exception on newer bridge firmwares ([#302](#302)) ([#309](#309)) (thx)
* Updated dependencies to the latest versions
* Fixed some typos here and there
@isaac-the-newt
Copy link

Hi Foddy,

Thanks for the update.
Alas version 4.2.0 appears to have introduced another error when commanding the lights....
TypeError: Cannot read properties of undefined (reading 'apply')
As well, the bridge is reconnecting continually spamming the log file (log set to INFO level logging).

21 Mar 11:00:38 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:00:38 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:00:38 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:00:39 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:00:39 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:00:39 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:00:39 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:00:39 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:00:40 - [info] [hue-bridge:Philips hue] [object Object]
21 Mar 11:00:40 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:00:40 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:00:40 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:00:40 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:00:41 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:00:41 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:00:41 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:00:41 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:00:49 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:00:49 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:00:49 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:00:49 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:00:49 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:00:49 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:00:50 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:00:50 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:00:50 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:00:50 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:00:51 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:00:51 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:00:51 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:00:51 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:00:59 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:00:59 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:00:59 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:00 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:01:00 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:01:01 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:01:01 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:01:10 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:10 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:01:10 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:10 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:10 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:11 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:01:11 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:01:11 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:01:11 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:01:12 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:01:12 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:01:12 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:01:12 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:01:12 - [error] [hue-light:Dining Table 1] TypeError: Cannot read properties of undefined (reading 'apply')
21 Mar 11:01:12 - [error] [hue-light:Kitchen Bench] TypeError: Cannot read properties of undefined (reading 'apply')
21 Mar 11:01:12 - [error] [hue-light:Backroom] TypeError: Cannot read properties of undefined (reading 'apply')
21 Mar 11:01:12 - [error] [hue-light:Dining Table 2] TypeError: Cannot read properties of undefined (reading 'apply')
21 Mar 11:01:20 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:01:20 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:20 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:01:20 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:20 - [info] [hue-bridge:Philips hue] Error: Request failed with status code 503
21 Mar 11:01:21 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:22 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:01:22 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:01:22 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:01:22 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:01:30 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:01:30 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:30 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:32 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:01:32 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:01:32 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:01:32 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:01:51 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:01:51 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:51 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:51 - [info] [hue-bridge:Philips hue] [object Object]
21 Mar 11:01:51 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:01:51 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:01:52 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:01:53 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:01:53 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:01:53 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:01:53 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:02:01 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:02:01 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:02:01 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:02:03 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:02:03 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:02:03 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:02:03 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…
21 Mar 11:02:22 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:02:22 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:02:22 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:02:23 - [info] [hue-bridge:Philips hue] Error requesting info from the bridge. Reconnect in some secs. undefined
21 Mar 11:02:23 - [info] [hue-bridge:Philips hue] Initializing the bridge (192.168.0.150)…
21 Mar 11:02:23 - [info] [hue-bridge:Philips hue] Connected to bridge
21 Mar 11:02:23 - [info] [hue-bridge:Philips hue] Processing bridge resources…
21 Mar 11:02:23 - [info] [hue-bridge:Philips hue] Initial emit of resource states…
21 Mar 11:02:24 - [info] [hue-bridge:Philips hue] Keeping nodes up-to-date…
21 Mar 11:02:24 - [info] [hue-bridge:Philips hue] Subscribing to bridge events…

@ptweety
Copy link
Contributor

ptweety commented Mar 21, 2022

Hi @isaac-the-newt,
better open up a new issue with your log, since yours is a different error as originally reported here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.