使用变量时无法从数组中提取值

时间:2017-11-20 08:47:19

标签: json alexa-skill

我一直在尝试将这种自定义Alexa技能写入一天,但我却陷入了非常小的困境。使用变量时,我无法从数组中输入检索值。代码places[theplace].pincode返回UNDEFINED。

任何指针?这是我正在使用的完整代码。

// 1. Text strings

var languageStrings = {
    'en': {
        'translation': {
            'WELCOME' : "Welcome to Pincode search!",
            'HELP'    : "Hi! you can ask me the pin code for any place you want. For example say Tell me the pin code for mumbai",
            'ABOUT'   : "This is an Alexa Skill.",
            'STOP'    : "Bye!"
        }
    }
};

var places = {
    "mumbai": {
        "pincode": "400001",
    },
    "delhi": {
        "pincode": "110001",
    },
};

// 2. Skill Code

var Alexa = require('alexa-sdk');
exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    // alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
    alexa.resources = languageStrings;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
    'LaunchRequest': function () {
        this.emit(':tell', this.t('WELCOME') + ' ' + this.t('HELP'));
    },

    'getPlaceIntent': function () {
        var theplace = this.event.request.intent.slots.place.value;
        var thepincode = places[theplace]['pincode'];
        this.emit(':tell', 'The pin code for '+ theplace + ' is ' + thepincode);

    },

    'AMAZON.HelpIntent': function () {
        this.emit(':ask', this.t('HELP'));
    },
    'AMAZON.NoIntent': function () {
        this.emit('AMAZON.StopIntent');
    },
    'AMAZON.CancelIntent': function () {
        this.emit(':tell', this.t('STOP'));
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', this.t('STOP'));
    }
};

1 个答案:

答案 0 :(得分:0)

这是固定的。这是工作代码:

// 1. Text strings
var languageStrings = {
    'en': {
        'translation': {
            'WELCOME' : "Welcome to Pincode search!",
            'HELP'    : "You can ask me the pin code for any place. For example, you can say 'Pin code for Mumbai'",
            'ABOUT'   : "This is an Alexa Skill created with NodeJS.",
            'STOP'    : "Bye!"
        }
    }
};

var places = {
    "mumbai": {
        "pincode": "400001",
    },
    "delhi": {
        "pincode": "110001",
    },
};

// 2. Skill Code

var Alexa = require('alexa-sdk');
exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    // alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
    alexa.resources = languageStrings;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
    'LaunchRequest': function () {
        this.emit(':tell', this.t('WELCOME') + ' ' + this.t('HELP'));
    },

    'getPlaceIntent': function (intent) {
        var theplace = this.event.request.intent.slots.place.value.toLowerCase();
        var thepincode = places[theplace].pincode;
        this.emit(':tell', 'The pin code for ' + theplace + ' is ' + thepincode + '.');
    },

    'AMAZON.HelpIntent': function () {
        this.emit(':ask', this.t('HELP'));
    },
    'AMAZON.NoIntent': function () {
        this.emit('AMAZON.StopIntent');
    },
    'AMAZON.CancelIntent': function () {
        this.emit(':tell', this.t('STOP'));
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', this.t('STOP'));
    }
};