0

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

Tom
  • 17,103
  • 8
  • 67
  • 75
Gawndy
  • 149
  • 1
  • 8

1 Answers1

1

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);
  });
Community
  • 1
  • 1
Tom
  • 17,103
  • 8
  • 67
  • 75