Search code examples
javascriptdictionaryaws-lambdaalexa-skills-kit

Dictionaries in Javascript for Alexa


I am building an Alexa skill using javascript (specifically Node.js) and I am running into an error that I do not understand. First, the code:

"use strict";

var Alexa = require("alexa-sdk");

var handlers = {
  "LaunchRequest": function () {
    this.response.speak(content[one]).listen(content[oneNext]); 
    this.emit(':responseReady');
  },
  "TwoIntent": function() {
    this.response.speak(content[two]).listen(content[twoNext]); 
    this.emit(':responseReady');
  }
};

var content = {

      one: 'text',
      oneNext: 'text',
      two:'text',
      twoNext: 'text',
      three: 'text',
      threeNext: 'text',
      four: 'text',
      fourNext: 'text',
      five: 'text',

    };

// Stock Alexa Handlers & Functions

exports.handler = function(event, context, callback){
  var alexa = Alexa.handler(event, context);
    alexa.registerHandlers(handlers);
    alexa.execute();
};

The error that I am getting in the Lambda interface is: "one is not defined. please fix or add /global one/ "

According to all of the documentation I could find on how to do dictionaries in javascript this syntax is correct. Is this something specific to alexa or lambda?


Solution

  • Instead of content[one] use content.one and content.oneNext and content.two and content.twoNext. Or possibly content["one"] ... that's the alternate syntax.