# IoT Hub crashed Node-red

**URL:** <https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123>\
**Category:** General\
**Created:** [27 June 2018 14:36 UTC](https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123 "2018-06-27T14:36:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)\
**Post date:** [27 June 2018 14:36 UTC](https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123/1 "2018-06-27T14:36:04Z")

</div>

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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [27 June 2018 14:41 UTC](https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123/2 "2018-06-27T14:41:19Z")

</div>

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](http://flows.nodered.org))

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

---

<div class="post-metadata">

**Author:** ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)\
**Post date:** [27 June 2018 14:43 UTC](https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123/3 "2018-06-27T14:43:29Z")

</div>

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

I will post it up now.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 June 2018 14:52 UTC](https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123/4 "2018-06-27T14:52:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 June 2018 15:19 UTC](https://discourse.nodered.org/t/iot-hub-crashed-node-red/1123/5 "2018-06-27T15:19:52Z")

</div>

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

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

```

and in the payload structure,

```auto
data: JSON.stringify(myData)

```

instead of

```auto
data: {
..
}

```
