我在下面的代码中的URL的另一端有一个休息服务,它返回s3存储桶中所有对象的名称。我试图创建一个简单的Alexa技能,告诉我该桶中有多少个对象。我的问题是,在http获取请求中,术语“this”没有引用与http获取请求外部相同的内容。如何在get请求中使用“this”或从我的http get请求中返回“count”变量?如何在回调中使用“this”?
"use strict";
var Alexa = require("alexa-sdk");
var Client = require('node-rest-client').Client;
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'Launch Request': function() {
this.emit('CountItems');
},
'ContentsIntent': function() {
this.emit('CountItems');
},
'CountItems': function() {
var client = new Client();
var count = undefined;
client.get("URL", function (data, response) {
console.log(data['Keys'].length);
count = data['Keys'].length;
});
this.emit(':tell', 'There are ' + count + ' items in your s3 bucket.');
}
};
的可能的复制[如何访问正确的\'这\'回调里面?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a -callback) – Pineda