简体   繁体   English

Alexa意图不等待api响应

[英]Alexa intent is not waiting for the api response

We are developing alexa skill using alexa-app, in one of our intent we are trying to fetch albums from facebook and on success/failure we want alexa to respond accordingly. 我们正在使用alexa-app开发alexa技能,我们的目的之一是尝试从Facebook获取相册,并且成功/失败后,我们希望alexa做出相应回应。 But intent is not waiting for FB call to complete. 但是意图不是等待FB调用完成。 Below is the code snippet we are using: 以下是我们正在使用的代码段:

function fetchAlbums(){
  return new Promise(resolve =>{
   FB.api("/me/albums", function (res) {
    if (res && !res.error) {
        // If we have data
        if (res.data) {
            resolve("Got Albums");
        } else {
            // REPORT PROBLEM WITH PARSING DATA
            resolve("Error getting albums");
        }
    } else {
        // Handle errors here.
        resolve("Error hitting endpoint");
    }
  });
 });
}

alexaApp.intent("readFeedIntent", {
  "utterances": [
    "read my facebook feed", "read facebook feed", "read feed"
  ]
},
function(request, res1) {
  // Again check if we have an access token
  if(request.hasSession() && !accessToken){
   accessToken = request.data.session.user.accessToken;
   FB.setAccessToken(accessToken);
  }
  if (accessToken) {
    var session = request.getSession();
    fetchAlbums().then(function(data){
        console.log(data);
        res1.say(data);
    });
  } else {
    res1.say(noAccessToken, tryLaterText).send();
  }
});

It is not throwing any errors, but Alexa is not speaking the anything where I can see the response in the console logs. 它没有引发任何错误,但是Alexa并没有说出我可以在控制台日志中看到响应的任何内容。

If I add: res1.say("Whatever!") at the end of function, then Alexa will speak 'Whatever' in response to this intent. 如果我在函数末尾添加: res1.say("Whatever!") ,则Alexa将针对此意图说出“ Whatever”。

Got it solved my myself: 得到了解决我自己的问题:

instead of this: 代替这个:

fetchAlbums().then(function(data){
      console.log(data);
      res1.say(data);
})

you have to return it, like: 您必须将其退回,例如:

return fetchAlbums().then(function(data){
      console.log(data);
      res1.say(data);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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