Node.js 无法处理alexa意图

Node.js 无法处理alexa意图,node.js,promise,alexa,alexa-app,alexa-sdk-nodejs,Node.js,Promise,Alexa,Alexa App,Alexa Sdk Nodejs,我的代码没有运行,有人能帮忙吗。 无法说出文本,我能否返回处理程序输入响应。测试函数是一个http调用,可能需要使用tieme function test(url, number) { return 5; } function speak(handlerInput) { return handlerInput.responseBuilder .getResponse(); } const NumberFactIntentHandler = { canH

我的代码没有运行,有人能帮忙吗。
无法说出文本,我能否返回处理程序输入响应。测试函数是一个http调用,可能需要使用tieme

function test(url, number)
{
    return 5;
}

function speak(handlerInput) {
    return handlerInput.responseBuilder
        .getResponse();
}

const NumberFactIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'NumberFactIntent';
    },

    handle(handlerInput) {

    const theNumber = handlerInput.requestEnvelope.request.intent.slots.number.value;
    const repromptOutput = " Would you like another fact?";
    const URL = "http://numbersapi.com";

    test(URL, theNumber).then(function (data) {
             console.log("data is " + data);
             handlerInput.responseBuilder
            .speak("Test Data")
            .reprompt(repromptOutput) 

             return speak(handlerInput);
        }).catch(function (data) {

             console.log("error is " + data);
             handlerInput.responseBuilder
            .speak(`I wasn't able to find a fact for ${theNumber}` )
            .reprompt(repromptOutput)
             return speak(handlerInput);
        }); 
    }
};

首先,您的
测试
函数不会返回承诺。我不知道这是否是有意的,您只是为了简化api调用代码,但如果您想在其上使用
,那么它应该返回一个承诺

如果它在您的完整示例中返回了一个承诺,那么您缺少的是在
test
之前添加一个返回。此外,您还应在承诺的范围内返回
handlerInput
。代码应该是这样的(我将删除一些不相关的代码):


现在,您可能想知道为什么需要这些
返回值。这是因为JS函数隐式返回
未定义的
,所以在这种情况下,您必须明确告诉
句柄
函数应该返回什么。这同样适用于承诺的内部。

此代码可能会帮助您

 //use request for http call
 function fun(url) {
      return new Promise((resolve, reject) => {
       request.get(url,(err, res, body) => {
       console.log("url-fetched");
       return resolve(body);
     });
   });
  }

   const NumberFactIntentHandler = {
      canHandle(handlerInput) {..
      },

 async handle(handlerInput) {

   const theNumber =handlerInput.requestEnvelope.request.intent.slots.number.value;
   const repromptOutput = " Would you like another fact?";
   const URL = "http://numbersapi.com";
   let data = await fun(url);

   return handlerInput.responseBuilder
    .speak(data)
    .reprompt('is there any thing i can do for you?')
    .withSimpleCard('Hello', speechText)
    .getResponse();
  };

“测试函数是一个可能需要时间的http调用”-http调用需要多少时间。Alexa将在8秒后超时,因此您需要在这之前返回响应。如果您正在将代码部署到AWS Lambda,我还建议您检查CloudWatch日志是否有任何错误。“现在,如果您仍然希望使用承诺,您可以。在您的情况下,您缺少的是在测试之前添加一个返回。”-我认为你应该更清楚地表明这可能是答案。我觉得在解释承诺时有点迷失了方向。另外,代码示例中的
test
函数不会返回承诺,因此
将不起作用-可能还需要明确说明该函数需要返回承诺(添加
async
当然会起作用)。感谢您的反馈。昨天我写了一个答案,真是欣喜若狂。用新的眼光看它,我发现答案的意义可能会消失。我根据你的评论更新了我的答案。
 //use request for http call
 function fun(url) {
      return new Promise((resolve, reject) => {
       request.get(url,(err, res, body) => {
       console.log("url-fetched");
       return resolve(body);
     });
   });
  }

   const NumberFactIntentHandler = {
      canHandle(handlerInput) {..
      },

 async handle(handlerInput) {

   const theNumber =handlerInput.requestEnvelope.request.intent.slots.number.value;
   const repromptOutput = " Would you like another fact?";
   const URL = "http://numbersapi.com";
   let data = await fun(url);

   return handlerInput.responseBuilder
    .speak(data)
    .reprompt('is there any thing i can do for you?')
    .withSimpleCard('Hello', speechText)
    .getResponse();
  };