Javascript Alexa技能错误

Javascript Alexa技能错误,javascript,alexa-skills-kit,Javascript,Alexa Skills Kit,我试图用我的Alexa技能调用第三方API,我在CloudWatch日志中得到了一个“会话以原因结束:错误”。问题似乎出在我的NumberIntentHandler或httpGet函数中,但我不确定在哪里 更新代码 --被解雇的处理程序-- const NumberIntentHandler = { canHandle(handlerInput) { return handlerInput.requestEnvelope.request.type === 'IntentReque

我试图用我的Alexa技能调用第三方API,我在CloudWatch日志中得到了一个“会话以原因结束:错误”。问题似乎出在我的NumberIntentHandler或httpGet函数中,但我不确定在哪里

更新代码

--被解雇的处理程序--

  const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      var options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  const myResult = httpGet(options);
         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
  },
};
--从处理程序调用的函数--

  async function httpGet(options) {
  // return new pending promise
  console.log(`~~~~~~~~~ httpGet ~~~~~~~~~`);
  console.log(`~~~~~${JSON.stringify(options)}~~~~~`);
  return new Promise((resolve, reject) => {    

  const request = http.get(options, (response) => {
      // handle http errors
      if (response < 200 || response > 299) {
        reject(new Error('Failed to load page, status code: ' + response));
      }// temporary data holder
      const body = [];
      // on every content chunk, push it to the data array
      response.on('data', (chunk) => body.push(chunk));
      // we are done, resolve promise with those joined chunks
      response.on('end', () => resolve(body.join('')));
      console.log('body: ', body[0]);
    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err));    
    request.end(); 
  });
}
const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      var options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  //const myResult = httpGet(options);
    const myResult = httpGet(options, res => {

         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
    });         
  },
};
function httpGet(options, cb) {
  http.get(options, res => {
    console.log(`~~~~~${JSON.stringify(options)}~~~~~`);
    // simplified version without error handling
    let output = []; 
    res.on('data', d => output.push(d)); // or concat to a string instead?
    res.on('end', () => cb(output));
    console.log('output: ', output[0]);
  });
}
--功能--

  async function httpGet(options) {
  // return new pending promise
  console.log(`~~~~~~~~~ httpGet ~~~~~~~~~`);
  console.log(`~~~~~${JSON.stringify(options)}~~~~~`);
  return new Promise((resolve, reject) => {    

  const request = http.get(options, (response) => {
      // handle http errors
      if (response < 200 || response > 299) {
        reject(new Error('Failed to load page, status code: ' + response));
      }// temporary data holder
      const body = [];
      // on every content chunk, push it to the data array
      response.on('data', (chunk) => body.push(chunk));
      // we are done, resolve promise with those joined chunks
      response.on('end', () => resolve(body.join('')));
      console.log('body: ', body[0]);
    });
    // handle connection errors of the request
    request.on('error', (err) => reject(err));    
    request.end(); 
  });
}
const NumberIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'NumberIntent';
  },
  handle(handlerInput) {
      let slotNum = handlerInput.requestEnvelope.request.intent.slots.number.value;
      //var myRequest = parseInt(slotNum);   
      const myRequest = parseInt(slotNum);
      console.log('NumberIntentHandler myRequest: ', myRequest);
      var options = `http://numbersapi.com/${myRequest}`;
      console.log('NumberIntentHandler options: ', options);

      // Use the async function
  //const myResult = httpGet(options);
    const myResult = httpGet(options, res => {

         console.log("sent     : " + options);
         console.log("received : " + myResult);
         const speechText = myResult;
         console.log('speechText: ', speechText); // Print the speechText   */ 

      return handlerInput.responseBuilder
           .speak(speechText)
           .withSimpleCard('Here is your fact: ', speechText)
           .getResponse(); 
    });         
  },
};
function httpGet(options, cb) {
  http.get(options, res => {
    console.log(`~~~~~${JSON.stringify(options)}~~~~~`);
    // simplified version without error handling
    let output = []; 
    res.on('data', d => output.push(d)); // or concat to a string instead?
    res.on('end', () => cb(output));
    console.log('output: ', output[0]);
  });
}

我相信您需要在httpGet中致电resolve并给出回复

作为旁注(与您的问题无关)-我可以建议使用请求承诺,它围绕http实现了一个非常好的承诺api,在这种情况下可以简化您的代码。(我知道我知道,async/await是新的、有趣的工具,但在本例中,我会选择“simpler”:)

另外,如果我没记错的话,http.get的回调只被一个参数调用

修改后编辑:

您可以去掉promise和async来简化代码。 请注意async/await-如果awaited表达式不是承诺,那么它将自动转换为承诺。在当前代码中,您要么需要像承诺一样使用它(例如chain a.then()),要么等待它

无论如何,下面是一个仅使用回调的示例:

function httpGet(options, cb) {
  http.get(options, res => {
    // simplified version without error handling
    let output = []; 
    res.on('data', d => output.push(d)); // or concat to a string instead?
    res.on('end', () => cb(output));
  });
}

httpGet(options, res => {
// building the alexa response, all your intent handler code that needs the response from your request
})

那么您的意思是我需要使用
constmyresult=wait-httpGet(resolve)
?抱歉,我不知道你的真正意思,因为这对我来说是新的。我的意思是,你没有解决你从httpGet返回的承诺。在新承诺的某个地方,您需要调用resolve()。您已经在调用reject,您只需要在我的处理程序的日志中添加要解析的调用(例如,在request.end()之后),我将得到“received:[object Promise]”,然后是speechText:Promise{}。异步函数是否应该分为两个函数,而其中一个函数基本上是一个返回承诺的同步函数,另一个是使用wait的异步函数?好的,现在有点混淆了:)您对请求的使用基本上是一个同步操作,我想我们可以简化一下——我会用我认为可行的解决方案更新我的答案。