【问题标题】:How would I go about using http in my alexa skill using alexa-sdk?我将如何使用 alexa-sdk 在我的 alexa 技能中使用 http?
【发布时间】:2017-02-05 22:49:20
【问题描述】:

我目前正在使用 node.js (Alexa SDK),但在发送 get 请求时遇到问题。我的请求如下所示:

 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);
});

如您所见,在“结束”回调中,我不能使用标准的“this.emit”,因为“this”指的是该上下文中的其他内容。我对如何解决这个问题有点困惑。有人可以帮忙吗?

谢谢

【问题讨论】:

    标签: javascript node.js this


    【解决方案1】:

    我相信这个问题是关于在回调中使用this,与ASK无关。

    您可以在此处找到有关该问题的完整讨论:
    How to access the correct `this` context inside a callback?

    解决这个问题的一个好办法是为你的回调使用胖箭头函数语法...

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

    【讨论】:

    • 谢谢。我不知道这是一件事!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多