2017-04-15 8 views
-3

私はs3バケット内のすべてのオブジェクトの名前を返す私のコードの下のURLの反対側に休憩サービスを持っています。私はそのバケツにいくつのオブジェクトがあるかを教えるシンプルなAlexaスキルを作ろうとしています。私の問題は、http get要求内で、 "this"という用語がhttp get要求の外にあるものと同じものを参照していないということです。どうすればgetリクエストの中で "this"を使うことができますか?あるいは、httpリクエストから "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.'); 
    } 
}; 
+1

[コールバック内の正しい\ 'this \'にアクセスするにはどうすればいいですか?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a -callback) – Pineda

答えて

1

それはあなたがthis値がなりたいものに依存します。 client.get()が呼び出される直前の値にしたい場合は、client.get()の前にローカル変数に値を保存してから、thisの代わりに保存した値を参照するのが普通の方法です(ES6以前)。あなたもそこに.emit()を配置する必要がありますので、また、あなたは、カウントを使用することができる唯一の場所は、コールバックの内側にある:

var handlers = { 
    'Launch Request': function() { 
     this.emit('CountItems'); 
    }, 
    'ContentsIntent': function() { 
     this.emit('CountItems'); 
    }, 
    'CountItems': function() { 
     var client = new Client(); 
     var self = this; 
     client.get("URL", function (data, response) { 
      let count = data['Keys'].length; 
      self.emit(':tell', 'There are ' + count + ' items in your s3 bucket.'); 
     }); 
    } 
}; 

あなたが使用することもでき.bind():ES6で

var handlers = { 
    'Launch Request': function() { 
     this.emit('CountItems'); 
    }, 
    'ContentsIntent': function() { 
     this.emit('CountItems'); 
    }, 
    'CountItems': function() { 
     var client = new Client(); 
     client.get("URL", function (data, response) { 
      let count = data['Keys'].length; 
      this.emit(':tell', 'There are ' + count + ' items in your s3 bucket.'); 
     }.bind(this)); 
    } 
}; 

(現代Node.jsのバージョン)、あなたは矢印機能を使用してコールバックを定義することができ、かつ、コールバック内thisの字句値を使用します。

var handlers = { 
    'Launch Request': function() { 
     this.emit('CountItems'); 
    }, 
    'ContentsIntent': function() { 
     this.emit('CountItems'); 
    }, 
    'CountItems': function() { 
     var client = new Client(); 
     client.get("URL", (data, response) => { 
      let count = data['Keys'].length; 
      this.emit(':tell', 'There are ' + count + ' items in your s3 bucket.'); 
     }); 
    } 
}; 

ES6環境では、ほとんどの人が矢印機能を最もクリーンな方法で実行しています。

+0

非ES6の場合、bindもオプションです:function(data、response){...} .bind(this) – Sasang

+0

@ChrisBell - あなたのクライアントに 'err'パラメータがあるとは思いませんか? .get() 'コールバック? – jfriend00

+0

@ササン - 私もそのオプションを追加しました。 – jfriend00

0

可変カウント内部とクロージャの外側の両方利用可能であるべきです。 あなたは、get-クロージャのスコープ内このの外側の文脈を使用したい場合は、あなたができる:

var that = this; 
client.get("URL", function (data, response) { 
     console.log(data['Keys'].length); 
     count = data['Keys'].length; 
     that.emit(':tell', 'There are ' + count + ' items in your s3 bucket.'); 
}); 
関連する問題