在 http 请求的响应中访问 'this' | Node.js

Accessing 'this' in http request's response | Node.js

我正在研究 Alexa 技能并使用 Node.js 编写我的 lambda 函数。我还使用 Alexa SDK 来处理 Alexa 的请求和响应。 我面临的问题是我无法访问 "this pointer" 作为响应。代码如下:

var handler = Alexa.CreateStateHandler();
handler['hi'] = function( ) {

console.log("hi intent");

    request(url, function (error, response, body) {

       const message = "Hello there. Best of luck!";
       const reprompt = "Hello there.";

       this.response.speak(message).listen(reprompt);
       this.emit(':responseReady');
    });        
};

错误:this.response 未找到

仅供参考:handler['hi'] 是一个 listener 并从 Alexa 自动调用。我可以在请求块之外访问这个指针。

我在 SO 上阅读了不同的答案,但其中 none 回答了我的问题。请帮助,我如何访问 "this" 内部响应。

你的问题确实是一个 javascript 问题。 js 'this' 不同于您可能习惯使用 c/c++ 等语言,您在这里遇到的问题 - 从回调中访问 'this' - 是一个常见的 js 陷阱所以已经很好地回答了,例如

How to access the correct `this` context inside a callback?