3

我试图在 lambda 函数中获取 aws iot 资源的影子,但给定的代码在成功时给出空值而不是数据。请让我知道问题出在哪里以及我应该做哪些更改才能使其正常工作。提前致谢。

var AWS=require('aws-sdk');
var iotdata = new AWS.IotData({endpoint: 'XXXXXXXXX.iot.us-east-1.amazonaws.com'});
var params = {
thingName: 'thing_name' /* required */
};

exports.handler=function(event,context){
payload1=new Buffer(event.payload);
console.log(payload1); 
iotdata.getThingShadow(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response

  context.succeed(data);

});

};
4

1 回答 1

0

需要改变的地方很少。这是新代码:

getIotShadow: function (thingName) {
    config.IOT_BROKER_ENDPOINT = "xxxxxxxxx.iot.us-east-1.amazonaws.com"; // also called the REST API endpoint
    config.IOT_BROKER_REGION = "us-east-1"; // eu-west-1 corresponds to the Ireland Region.  Use us-east-1 for the N. Virginia region
    config.IOT_THING_NAME = thingName;
    AWS.config.region = config.IOT_BROKER_REGION;
    var iotData = new AWS.IotData({
        endpoint: config.IOT_BROKER_ENDPOINT
    });

    var paramsGet = {
        "thingName": config.IOT_THING_NAME /* required */
    };

    iotData.getThingShadow(paramsGet, function (err, data) {
        if (err) {
            console.log("Error : " + err, err.stack);
        } else {
            console.log(JSON.stringify(data));
        }
    });
}
于 2018-06-19T11:25:42.977 回答