2
votes

Im using the Node.JS libraries for Azure IoT Hub to send some telemetry data, and intermittently I get the following error thrown from the libraries.

Unhandled rejection TypeError: Cannot read property 'on' of undefined at C:\Source\Messenger\app.js:156:17 at Amqp. (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\lib\amqp.js:157:17) at C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\lib\amqp.js:54:19 at C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\lib\amqp.js:84:17 at tryCatcher (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\util.js:11:23) at Promise._settlePromiseFromHandler (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\promise.js:489:31) at Promise._settlePromise (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\promise.js:546:18) at Promise._settlePromise0 (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\promise.js:591:10) at Promise._settlePromises (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\promise.js:670:18) at Async._drainQueue (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\async.js:129:16) at Async._drainQueues (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\async.js:139:10) at Immediate.Async.drainQueues [as _onImmediate] (C:\Source\Messenger\node_modules\azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10\node_modules\bluebird\js\release\async.js:16:14)

At the moment im running my scripts through forever so it recovers when the error is generated.

Has anyone else had this issue and managed to resolve it ?

My code looks like as follows

azureClient = azureDevice.Client.fromConnectionString(connectionString, azureDevice.Amqp);

var message = new azureDevice.Message(JSON.stringify('TEST MESSAGE'));

azureClient.sendEvent(message, function (err) {
    if (err != null) {
        Configure();
    }                        
});          
2

2 Answers

1
votes

For this issue, I recommend you can use the AMQPWS or HTTP transport to send and receive messages. Please refer to my steps to achieve to send and receive the message:

Firstly, we should create a device and get the device id and SAS. After created the IOT hub on Azure new portal, I ran those command on node.js command line: create IOT hub on new portal.

npm install -g iothub-explorer
iothub-explorer HostName=****.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=****/****= create mydevice --connection-string

I got the result as following:

Created device mydevice

-
  deviceId:                   mydevice
  generationId:               635881160181557968
  etag:                       MA==
  connectionState:            Disconnected
  status:                     enabled
  statusReason:               null
  connectionStateUpdatedTime: 0001-01-01T00:00:00
  statusUpdatedTime:          0001-01-01T00:00:00
  lastActivityTime:           0001-01-01T00:00:00
  cloudToDeviceMessageCount:  0
  authentication:
    SymmetricKey:
      primaryKey:   ****/****=
      secondaryKey: ****/****=
-
  connectionString: HostName=****.azure-devices.net;DeviceId=mydevice;SharedAccessKey=****/****=

Secondly, I installed the SDK on node.js project, the folder structure is same as this picture: enter image description here

Thirdly, I run this code on my project:

var device = require('azure-iot-device');
var connectionString = 'HostName=****.azure-devices.net;DeviceId=mydevice;SharedAccessKey=****/****=';
var client = device.Client.fromConnectionString(connectionString,device.AmqpWS);
setInterval(function () {
    var windSpeed = 10 + (Math.random() * 4); // range: [10, 14] 
    var data = JSON.stringify({ deviceId: 'mydevice', windSpeed: windSpeed });
    var message = new device.Message(data);
    message.properties.add('myproperty', 'myvalue');
    console.log("Sending message: " + message.getData());
    client.sendEvent(message, printResultFor('send'));   
}, 1000);

client.getReceiver(function (err, receiver) {
    receiver.on('message', function (msg) {
        console.log('Id: ' + msg.properties.messageId + ' Body: ' + msg.body);
        receiver.complete(msg, function () {
            console.log('completed');


             // receiver.reject(msg, function() { 
             //   console.log('rejected'); 
             // }); 
             // receiver.abandon(msg, function() { 
             //   console.log('abandoned'); 
             // }); 

        });
        receiver.on('errorReceived', function (err) {
            console.warn(err);

        });
    });
});

function printResultFor(op) {
    return function printResult(err, res) {
        if (err) console.log(op + ' error: ' + err.toString());
        if (res) console.log(op + ' status: ' + res);

    };

}

it works fine. Also if you used the "Http" transport, it also works fine. You need use this code:

var client = device.Client.fromConnectionString(connectionString);

This is the reason why I recommend you use the AMQPWS, because of this code in "azure-iot-device\node_modules\azure-iot-common\node_modules\amqp10-transport-ws":

if (this.uri.startsWith('wss')) {
    var wsTransport = require('amqp10-transport-ws');
    wsTransport.register(amqp10.TransportProvider);
  }

If the URI is format with 'wss', it will be register the AMQP transport. But AMQP transport's format is like "amqp://". Hope this helps.

0
votes

The Azure IoT Hub was by far a preview feature service on Azure. And the Azure IoT device SDK for Node.js is on the fast development.

I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay.

Appreciate your patience.