我正在尝试迭代alexa的语音输出
for(var i = 0; i< status.passengers.length; i++) {
console.log("----inside getJson--------->",status.passengers[i].current_status);
let reservationStauts = status.passengers[i].current_status;
statusWords = reservationStauts.split("/");
for(var j=0; j < statusWords.length; j++) {
confirmStatus = statusWords[0];
statusNumber = statusWords[2];
}
passenger = status.passengers[i].no;
speechOutput = "the p n r status for passenger "+passenger+" is "+confirmStatus+statusNumber;
callback(session.attributes, buildSpeechletResponse(cardTitle, speechOutput, "", false));
}
但由于回调改变了控制器,我无法获得语音输出的第二次迭代。
基本上我正在尝试迭代语音输出!
答案 0 :(得分:2)
这里Alexa并没有什么独特之处,相反,它是一个javascript和回调处理问题。诀窍是在绝对必要之前不要回电 在这种情况下,因为看起来您正在尝试构建包含多个模式信息的响应,所以可能最好在最外面的for循环之外移动回调。
我已经尝试更新下面的代码段:
注意:我还在循环外移动了speechOutput的声明,以便您可以通过循环的每次迭代构建它
let speechOutput = ''; // need to declare this outside the loop and treat the loop as your speech builder
for(var i = 0; i< status.passengers.length; i++) {
console.log("----inside getJson--------->",status.passengers[i].current_status);
let reservationStauts = status.passengers[i].current_status;
statusWords = reservationStauts.split("/");
for(var j=0; j < statusWords.length; j++) {
confirmStatus = statusWords[0];
statusNumber = statusWords[2];
}
passenger = status.passengers[i].no;
speechOutput += "the p n r status for passenger "+passenger+" is "+confirmStatus+statusNumber;
speechOutput += ',\n'; //SINCE you're looping over multiples - should probably throw some sort of speach separator/pause here
}
callback(session.attributes, buildSpeechletResponse(cardTitle, speechOutput, "", false));