如何将数据返回到节点js

时间:2018-06-18 15:10:21

标签: javascript node.js asynchronous alexa alexa-skills-kit

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  handle(handlerInput) {
      function getNameAndWelcome(callback) {
        request(amznProfileURL,function (error, response, body) {
          if (response.statusCode == 200) {
            var profile = JSON.parse(body);
            let name = profile.name.split(" ")[0];
            const speechText = `Welcome to the real estate skill ${name}, you can enquire about properties in various cities like Austin.`;
            Response = handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .withSimpleCard('Real Estate', speechText)
            .getResponse();
          }else{
            const speechText = `Welcome to the real estate skill, you can enquire about properties in various cities like Austin.`;
            Response = handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .withSimpleCard('Real Estate', speechText)
            .getResponse();
          }
          callback(Response);
        })
      }
      return getNameAndWelcome(function(res){
        return res;
      });
    }else{
      const speechText = 'Hi there! This skill requires account linking. Please follow the steps sent on the alexa application in your mobile.';

      return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withLinkAccountCard()
      .getResponse();
    } 
  }
};

这是一个代码片段,我整天都在敲打我的脑袋。我正在尝试在变量中返回对象"响应"到"处理" function.I希望从代码中清楚我已经尝试过。基本上我发送一个回调,我希望将回复"响应"反对函数" getNameAndWelcome"然后我可以再次返回到外部范围,但你可以猜测它不起作用。我在哪里弄错了?谢谢。

这是我注册处理程序的部分。

    exports.handler = async function (event, context) {
  if (!skill) {
    skill = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
      LaunchRequestHandler,
      LocationIntentHandler,
      StatusIntentHandler,
      GeneralIntentHandler,
      QAIntentHandler,
      LowestPriceIntentHandler,
      HighestPriceIntentHandler,
      HelpIntentHandler,
      CancelAndStopIntentHandler,
      SessionEndedRequestHandler,
      )
    .addRequestInterceptors(AccessTokenInterceptor)
    .addErrorHandlers(ErrorHandler)
    .withSkillId(skillId)
    .create();
  }

  return skill.invoke(event,context);
}

0 个答案:

没有答案