TypeError: _this._transport.sendEvent is not a function

TypeError: _this._transport.sendEvent is not a function

我已成功配置设备,我正在使用 X509 证书进行配置并且它工作正常。现在我想将设备消息发送到 IoT 中心,因此我创建了一个单独的 class DeviceHelper,但我收到此错误。有人可以帮忙吗? TypeError: _this._transport.sendEvent 不是一个函数

设备配置代码

var fs = require('fs');
var DeviceHelper = require('./device-helper');
var Transport = require('azure-iot-provisioning-device-http').Http;
var X509Security = require('azure-iot-security-x509').X509Security;
var ProvisioningDeviceClient = require('azure-iot-provisioning- 
device').ProvisioningDeviceClient;
// Register the device.  Do not force a re-registration.
        deviceClient.register(function (err, result) {
           if (err) {
               that.setProvisionStatus(PROVISION_STATUS.ERROR);
            console.log(err);
        } else {
            console.log('registration succeeded');
            that.assignedHub = result.assignedHub;
            console.log('assigned hub=' + result.assignedHub);
            console.log('deviceId=' + result.deviceId);
            var deviceHelper = new DeviceHelper(that.deviceId, that.assignedHub, deviceCert, Transport);
            deviceHelper.sendEvent();
        }
    });

DeviceHelper代码:

   

 

var Device = require('azure-iot-device');
    var Client = Device.Client;
    var X509AuthenticationProvider = Device.X509AuthenticationProvider;
    var Message = Device.Message;
    var Http = Device.Http;

    var client = {};
    class DeviceHelper {

      constructor(deviceId, hubName, security, transport) {
        client = Client.fromAuthenticationProvider(X509AuthenticationProvider.fromX509Options(
          deviceId, hubName, security
        ),transport);
      }

      print(err, res) {
        if (err) console.log(err.toString());
        if (res) console.log(res.statusCode + ' ' + res.statusMessage);
      }

      sendEvent() {
    // Error on this line
        client.sendEvent(new Message('hello world'), this.print);
      }
    }

    module.exports = DeviceHelper;

问题的控制台日志:

TypeError: _this._transport.sendEvent is not a function
index.js:23
message:"_this._transport.sendEvent is not a function"
stack:"TypeError: _this._transport.sendEvent is not a function\n at D:\Working\Brahma\web-communicator\server\iot-agent\node_modules\azure-iot-device\lib\client.js:216:30\n at retryOperation (D:\Working\Brahma\web-communicator\server\iot-agent\node_modules\azure-iot-device\node_modules\azure-iot-common\lib\retry_operation.js:31:13)\n at RetryOperation.retry (D:\Working\Brahma\web-communicator\server\iot-agent\node_modules\azure-iot-device\node_modules\azure-iot-common\lib\retry_operation.js:57:9)\n at Client.sendEvent (server\iot-agent\node_modules\azure-iot-device\lib\client.js:214:17)\n at DeviceHelper.sendEvent (\device-helper.js:22:12)\n at \iot-device.js:99:30\n at D:\Working\Brahma\web-communicator\server\iot-agent\node_modules\azure-iot-provisioning-device\lib\x509_registration.js:50:33\n at constructor._onEnter (D:\Working\Brahma\web-communicator\server\iot...
proto:Error {constructor: , name: "TypeError", message: "", …}

我建议您看一下 azure-iot-sdk-node/device/samples/simple_sample_device.js,它提供了一个很好的示例,说明如何使用 SDK.

发送消息

希望对您有所帮助!

您发送给 deviceHelper 构造函数的传输似乎是 供应 传输。我想你想发送一个设备客户端传输。 deviceHelper 模块的代码片段确实需要设备传输。也就是那种有send的传输方式。