AWS IOT:无法注册阴影

时间:2017-02-24 10:45:11

标签: aws-iot

我在AWS IoT中创建了一个名为RasberryPi的东西,我试图使用以下代码注册它的阴影:

var awsIot = require('aws-iot-device-sdk');

var thingShadows = awsIot.thingShadow({
   keyPath: "./certs/private.pem.key",
   certPath: "./certs/certificate.pem.crt",
   caPath: "./certs/root-CA.crt",
   clientId: "RasberryPi1",
   region: "eu-west-1",
   port: "8883",
   debug: true
});

thingShadows.on('connect', function() {

    console.log('connected');

    thingShadows.register( 'RasberryPi1', function() {
        console.log('registered');
...

我成功连接但我从未注册过。也就是说,第二个console.log语句永远不会运行。任何人都可以告诉我我的错误吗?我是AWS IoT的新手,我的错误可能非常基本。

非常感谢

1 个答案:

答案 0 :(得分:0)

这可能真的令人困惑,但这是因为文档。您可能正在关注此处的示例:https://github.com/aws/aws-iot-device-sdk-js#thing-shadow-class

//
// After connecting to the AWS IoT platform, register interest in the
// Thing Shadow named 'RGBLedLamp'.
//
thingShadows.register( 'RGBLedLamp', function() {
    // Your code here
}

但是当你在这里阅读文档时:https://github.com/aws/aws-iot-device-sdk-js#awsiotthingshadowregisterthingname-options-callback-你可以看到注册函数不是作为第二个参数而是作为第三个(!)

进行回调

就这样称呼它:

 thingShadows.register( 'RGBLedLamp', {}, function() {
    // Your code here
}

它会像魅力一样工作