【问题标题】:Alexa Node.js Skills Kit - Need to return callback data before handler is completedAlexa Node.js 技能工具包 - 需要在处理程序完成之前返回回调数据
【发布时间】:2016-09-20 17:21:32
【问题描述】:

我正在尝试构建一个简单的 Alexa 技能,以使用 [Node.js ASK] (https://developer.amazon.com/public/community/post/Tx213D2XQIYH864/Announcing-the-Alexa-Skills-Kit-for-Node-js) 从 API 返回数据。我已将 http get 放在处理程序中,但 Alexa 在回调异步返回 API 数据之前完成了处理程序。

我一直在寻找答案,目前我的想法是:

  1. 不使用 node.js
  2. 想办法同步获取数据
  3. 我缺少一些简单的东西

代码核心:

exports.handler = function(event, context, callback) {
  var alexa = Alexa.handler(event, context);
  alexa.registerHandlers(handler);
  alexa.execute();
};

var handler = Alexa.CreateStateHandler(states.x, {
  'intent': function() {
    var options = {
      host: baseURL,
      path: pathURL
    };
    
    callback = function(response) {
      var str = "";
      response.on('data', function(piece) {
        str += piece;
      });
      
      response.on('end', function() {
        //does not get executed
        this.emit(':tell', str, "test");
      });
    }
    
    http.request(options, callback).end();
    
    //this does get executed if I leave this here
    this.emit(':tell'...);
  };

【问题讨论】:

    标签: alexa-skills-kit


    【解决方案1】:

    我认为您遇到了范围问题。 试试……

    response.on('end',() => {
        this.emit(':tell', str, "test");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      相关资源
      最近更新 更多