简体   繁体   中英

How would I go about using http in my alexa skill using alexa-sdk?

I'm currently using the node.js ( Alexa SDK ) and I'm having trouble sending a get request. Here's what my request looks like:

 http.get("http://graph.facebook.com/v2.7", function(res) {
        res.on('data', function (chunk) {
            temp += chunk;
        });

        res.on('end', function () {
            //Figure out how to not use "this" keyword because it doesn't work....
            this.emit(":ask", toAsk, temp);
        });
}).on('error', function (e) {
   console.log("Got error: ", e);
});

As you can see, in the "end" callback I can't use the standard "this.emit" because "this" refers to something else in that context. I'm a bit confused on how to get around this. Could someone help?

Thank you

I believe that this question is about the use of this in a callback and is not related to ASK.

You can find a full discussion of the issue here:
How to access the correct `this` context inside a callback?

A good solution to the problem would be to use the fat arrow function syntax for your callback...

res.on('end', () => {
       // with this syntax, 'this' is same as in above (res.on) context.
       this.emit(":ask", toAsk, temp);
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM