【问题标题】:I want to call var from another handler function我想从另一个处理函数调用 var
【发布时间】:2019-06-19 10:50:09
【问题描述】:

我正在做一个项目,我需要从另一个函数调用一个变量,但我不知道如何调用它这里是我想从 HelloWorldIntentHandler 调用 team1、team2、server 的代码我该如何调用?

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
        && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {

    var team1 =  handlerInput.requestEnvelope.request.intent.slots.score.value;
    var team2 =  handlerInput.requestEnvelope.request.intent.slots.scoreone.value;
    var server = handlerInput.requestEnvelope.request.intent.slots.scoretwo.value;
    var value1 = Number(team1);
    var value2 = Number(team2);
    var totalteam1 = Math.round(value1 + value1 );
    var totalteam2 = Math.round(value2 + value2 );
    return handlerInput.responseBuilder
        .speak("score is " + totalteam1)
        .reprompt()
        .getResponse();
}

};

const PickleballScoreNowHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
        && handlerInput.requestEnvelope.request.intent.name === 'PickleballScoreNow';
},
handle(handlerInput) {

    var team1 =  handlerInput.requestEnvelope.request.intent.slots.score.value;
    var team2 =  handlerInput.requestEnvelope.request.intent.slots.scoreone.value;
    var server = handlerInput.requestEnvelope.request.intent.slots.scoretwo.value;
    return handlerInput.responseBuilder
        .speak("score is " + team1 + team2 + server)
        .reprompt()
        .getResponse();
}

};

【问题讨论】:

  • 你能否在更多解释中把问题说清楚一点?

标签: javascript node.js function logic alexa


【解决方案1】:

你可以试试这个:

const HelloWorldIntentHandler = {
    var score={};

   canHandle(handlerInput) {
          return handlerInput.requestEnvelope.request.type === 'IntentRequest'
         && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
        },

 handle(handlerInput) {

**score.team1 =  handlerInput.requestEnvelope.request.intent.slots.score.value;
score.team2 =  handlerInput.requestEnvelope.request.intent.slots.scoreone.value;
score.server = handlerInput.requestEnvelope.request.intent.slots.scoretwo.value;**
var value1 = Number(team1);
var value2 = Number(team2);
var totalteam1 = Math.round(value1 + value1 );
var totalteam2 = Math.round(value2 + value2 );
return handlerInput.responseBuilder
    .speak("score is " + totalteam1)
    .reprompt()
    .getResponse();
 }

-- 你可以像这样访问你的所有变量。

  HelloWorldIntentHandler.score.team1

【讨论】:

  • 我收到错误 unexpected var in var score = {}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
  • 2019-04-02
相关资源
最近更新 更多