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 crashing Node-RED | When the connect action on MQTT is performed with undefined values #4380

Closed
mayurkhadse opened this issue Oct 11, 2023 · 3 comments · Fixed by #4454

Comments

@mayurkhadse
Copy link

Current Behavior

There comes an exception if the global variables are not defined and we try to read them and apply the configuration to MQTT via msg properties.

28 Sep 12:53:23 - [error] TypeError: Cannot read properties of undefined (reading 'indexOf')
at MQTTBrokerNode.node.setOptions (C:\Users###\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\nodes\core\network\10-mqtt.js:590:33)
at C:\Users###\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\nodes\core\network\10-mqtt.js:423:37
at _callback (C:\Users###\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\nodes\core\network\10-mqtt.js:834:62)
at C:\Users###\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\nodes\core\network\10-mqtt.js:869:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)`

Expected Behavior

There should be an error message if the global variables are not defined are being forced to the MQTT.

Steps To Reproduce

The reproduction flow is attached to this issue.

Example flow

[
{
"id": "2c726c6c8c237557",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "3270f5029c4593da",
"type": "function",
"z": "2c726c6c8c237557",
"name": "function 10",
"func": "//Configuration data for the mqtt\nmsg.action = "connect"\n\n\nmsg.broker = {\n "broker": global.get("HelowWorld"),\n "port": global.get("MqttPort"),\n "clientid": global.get("MqttClientId"),\n "username": global.get("MqttUserName"),\n "password": global.get("MqttPassword"),\n "force": true,\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1190,
"y": 380,
"wires": [
[
"929e99eb6924d1b5"
]
]
},
{
"id": "ee2f604b237e553a",
"type": "inject",
"z": "2c726c6c8c237557",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 840,
"y": 380,
"wires": [
[
"3270f5029c4593da"
]
]
},
{
"id": "929e99eb6924d1b5",
"type": "mqtt out",
"z": "2c726c6c8c237557",
"name": "Sending_Reset_Signals",
"topic": "",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "0ff7e11968ee8724",
"x": 1510,
"y": 460,
"wires": []
},
{
"id": "0ff7e11968ee8724",
"type": "mqtt-broker",
"name": "",
"broker": "123.125.123.10",
"port": "8883",
"tls": "4dc487b6.a817e8",
"clientid": "",
"autoConnect": true,
"usetls": true,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": "",
"credentials": {}
},
{
"id": "4dc487b6.a817e8",
"type": "tls-config",
"name": "Broker TLS",
"cert": "",
"key": "",
"ca": "",
"certname": "",
"keyname": "",
"caname": "server.pem",
"servername": "",
"verifyservercert": false,
"alpnprotocol": ""
}
]

Environment

  • Node-RED version: 3.0.2
  • Node.js version: 18.18.0
  • npm version: 10.2.0
  • Platform/OS: Windows
  • Browser: Chrome
@Steve-Mcl
Copy link
Contributor

Hi, thanks for reporting. I can confirm this happens in NR 3.0.2 as well.

Do you have a newer Node-RED v3.1.0 that you could test this on also?

@Steve-Mcl
Copy link
Contributor

There should be an error message if the global variables are not defined are being forced to the MQTT.

To be clear, this has nothing to do with global variables. The issue is because your code is trying to initialise an MQTT connection with bad (undefined) values.

The correct course of action would be to raise a catchable error (or output a "warn" to the sidebar) that invalid variables were provided in the connection object.

@Steve-Mcl
Copy link
Contributor

Updated flow demonstrating the issue (using undefined instead of global.get) to demonstrate the issue is not related to global vars

[{"id":"3270f5029c4593da","type":"function","z":"2c726c6c8c237557","name":"function 10","func":"//Configuration data for the mqtt\nmsg.action = \"connect\"\n\n// original code:\n// msg.broker = {\n//  \"broker\": global.get(\"HelowWorld\"),\n//  \"port\": global.get(\"MqttPort\"),\n//  \"clientid\": global.get(\"MqttClientId\"),\n//  \"username\": global.get(\"MqttUserName\"),\n//  \"password\": global.get(\"MqttPassword\"),\n//  \"force\": true,\n// }\n\n// updated to demonstrate\n// this has nothing to do with global vars\nmsg.broker = {\n    \"broker\": undefined,\n    \"port\": undefined,\n    \"clientid\": undefined,\n    \"username\": undefined,\n    \"password\": undefined,\n    \"force\": true,\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1170,"y":380,"wires":[["929e99eb6924d1b5"]]},{"id":"ee2f604b237e553a","type":"inject","z":"2c726c6c8c237557","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1000,"y":380,"wires":[["3270f5029c4593da"]]},{"id":"929e99eb6924d1b5","type":"mqtt out","z":"2c726c6c8c237557","name":"Sending_Reset_Signals","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"0ff7e11968ee8724","x":1390,"y":380,"wires":[]},{"id":"0ff7e11968ee8724","type":"mqtt-broker","name":"","broker":"123.125.123.10","port":"8883","tls":"4dc487b6.a817e8","clientid":"","autoConnect":true,"usetls":true,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"4dc487b6.a817e8","type":"tls-config","name":"Broker TLS","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"server.pem","servername":"","verifyservercert":false,"alpnprotocol":""}]

The exception:

TypeError: Cannot read properties of undefined (reading 'indexOf')
    at MQTTBrokerNode.node.setOptions (/usr/src/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:590:33)
    at /usr/src/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:423:37
    at _callback (/usr/src/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:834:62)
    at /usr/src/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:869:21
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

@Steve-Mcl Steve-Mcl changed the title Uncaught Exception | When the global variables are not defined, but used in a node Uncaught Exception crashing Node-RED | When the connect action on MQTT is performed with undefined values Oct 11, 2023
knolleary added a commit that referenced this issue Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants