使用变量查找值。 JSON Alexa技能

问题描述 投票:0回答:1

你好,我制作了一个简单的Alexa技能,要求一个怪物名字并告诉你速度

我询问用户一个怪物名称,然后用户的怪物名称存储在:handlerInput.requestEnvelope.request.intent.slots.name.value

并且我将其保存到变量monster(下面的代码),因此它保存了用户指定的名称。

我还有一个名为typecharts的文件,那里有关于怪物速度的数字。

typecharts文件:

module.exports = {

    Werewolf: '60',
    Alien: "98",
    Herb: "10",
}; 

so变量monster(下面的代码)正确打印用户给定的名称,问题是这样的:

  • [我知道如何调用该文件(typecharts)中的数字值,例如WerewolfAlien,以便alexa可以这样说:。speak(typecharts.Alien ),并显示98

  • 但是我需要放置一个变量,该变量存储用户给定的monters的名称,以便它查找我们想要的怪物,诸如此类的。speak(typecharts。variable monster here)]所以她会寻找用户说的任何类型的图表,无论是外星人的野狼还是草药。

我尝试通过创建此变量const spe =(typecharts.monster)进行尝试,但是失败了,因为它没有查找用户说的名称(保存在“ monster”变量),它看上去取而代之的是“ monster”,而不是变量中的名称,¿我如何使它查找存储在Monster上的文本?

要修复的代码:

    canHandle(handlerInput) {

        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'CompareIntent';
                            },

    handle(handlerInput) {

        const monster = handlerInput.requestEnvelope.request.intent.slots.name.value
        const spe = (typecharts.monster) // <- i need to fix this to do what i need.

        return handlerInput.responseBuilder

            .speak("you said "+ monster +" with speed " + spe ) // <- alexa speech output
            .reprompt("tell me more")
            .getResponse();

    }
json variables alexa alexa-skills-kit alexa-app
1个回答
0
投票
使用括号语法访问带有变量的对象键(否则,它将尝试找到名为“ monster”的键,而您没有该键)。

handle(handlerInput) { const monster = handlerInput.requestEnvelope.request.intent.slots.name.value const spe = (typecharts[monster]) return handlerInput.responseBuilder .speak("you said "+ monster +" with speed " + spe ) // <- alexa speech output .reprompt("tell me more") .getResponse(); }

© www.soinside.com 2019 - 2024. All rights reserved.