IoT Hub crashed Node-red

My server has now crashed from this flow:

[{"id":"beb2fc2f.9da3b","type":"tcp in","z":"53f246d7.f619f8","name":"","server":"client","host":"xxx","port":"xxx","datamode":"stream","datatype":"utf8","newline":"","topic":"WaveBuoy","base64":false,"x":150,"y":280,"wires":[["d0a41ac5.9f11e8"]]},{"id":"d0a41ac5.9f11e8","type":"function","z":"53f246d7.f619f8","name":"","func":"var parts = msg.payload.split(\",\");\n\nmsg.payload = {\n \"deviceID\": \"WaveBuoy\",\n \"SAK\": \"XXX=\",\n \"Protocol\": \"mqtt\",\n \"Data\": {\n \"Heave\": msg.payload[4],\n \"Northings\": msg.payload[5] \n //\"Eastings\": msg.payload[6] \n }\n};\n\nmsg.payload = JSON.stringify(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":280,"wires":[["a40d774a.bf8648","4073cbfd.cf8d14"]]},{"id":"a40d774a.bf8648","type":"azureiothub","z":"53f246d7.f619f8","name":"Azure IoT Hub","protocol":"mqtt","x":780,"y":120,"wires":[[]]}]

The error is as follows:

27 Jun 15:30:37 - [error] [azureiothub:Azure IoT Hub] TypeError: Cannot read property 'toString' of undefined
    at Mqtt.publish (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\azure-iot-mqtt-base\lib\mqtt.js:131:57)
    at Mqtt.sendEvent (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\azure-iot-device-mqtt\lib\mqtt.js:71:14)
    at Client.sendEvent (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\azure-iot-device\lib\client.js:271:19)
    at sendData (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\azureiothub.js:38:16)
    at sendMessageToIoTHub (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\azureiothub.js:65:13)
    at AzureIoTHubNode.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\azureiothub.js:159:13)
    at emitOne (events.js:116:13)
    at AzureIoTHubNode.emit (events.js:211:7)
    at AzureIoTHubNode.Node.receive (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\red\runtime\nodes\Node.js:215:14)
    at FunctionNode.Node.send (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\red\runtime\nodes\Node.js:202:14)
    at sendResults (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\nodes\core\core\80-function.js:52:18)
    at FunctionNode.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\nodes\core\core\80-function.js:221:21)
    at emitOne (events.js:116:13)
    at FunctionNode.emit (events.js:211:7)
    at FunctionNode.Node.receive (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\red\runtime\nodes\Node.js:215:14)
    at TcpIn.Node.send (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\red\runtime\nodes\Node.js:137:22)
    at Socket.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red\nodes\core\io\31-tcpin.js:76:34)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
27 Jun 15:30:38 - [error] [azureiothub:Azure IoT Hub] Could not connect: Connection refused: Not authorized
27 Jun 15:30:38 - [info] [azureiothub:Azure IoT Hub] Connected to Azure IoT Hub.
27 Jun 15:30:38 - [red] Uncaught Exception:
27 Jun 15:30:38 - TypeError: Cannot read property 'on' of undefined
    at C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\azureiothub.js:89:24
    at C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\azure-iot-device\lib\client.js:240:9
    at Mqtt.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\azure-iot-device-mqtt\lib\mqtt.js:45:17)
    at MqttClient.errCallback (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\azure-iot-mqtt-base\lib\mqtt.js:75:7)
    at emitNone (events.js:111:20)
    at MqttClient.emit (events.js:208:7)
    at TLSSocket.f (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\once\once.js:25:25)
    at TLSSocket.onend (C:\Users\admin\AppData\Roaming\npm\node_modules\node-red-contrib-azure-iot-hub\node_modules\end-of-stream\index.js:35:27)
    at emitNone (events.js:111:20)
    at TLSSocket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

It looks like an error with the contrib-node node-red-contrib-azure-iot-hub

You should open an issue on the nodes github page ( theres a link from the nodes page on flows.nodered.org)

What version of node-red and nodejs are you using?

Node-Red = v0.18.7
Node.js = v8.11.3

I will post it up now.

I suspect that the bug may be induced by the fact that you have passed the node a string whereas I suspect that it expects an javascript object. Try removing the JSON.stringify line from your function.

It is still a bug in the node though, no data that you pass should cause node red to crash.

Also I notice that you have not got the keys exactly as in the readme for the node, unless I am looking at the wrong node. You have deviceID instead of deviceId, Protocol instead of protocol, Data instead of data and SAK instead of key.

Note also the rather confusing fact that, according to the readme, the data item has to be a json string. If that is the case then you will need something like

myData = {Heave: msg.payload[4], Northings: msg.payload[5]};

and in the payload structure,

data: JSON.stringify(myData)

instead of

data: {
..
}