简体   繁体   中英

Unexpected Parsing Error On AWS Lambda JS

I think this is a syntax error but I'm having trouble finding documentation. I keep getting 'Parsing Error: Unexpected Token {". It says its to do with the 'YesIntent', but won't give specifics. I'm new to JS, but I can't see what could be the problem. Every '{' has a matching '}'.

Any insights would be appreciated. Thank you.

const Alexa = require("alexa-sdk");
const appId = ''; //'';

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

const handlers = {
    'LaunchRequest': function() {
      this.emit('YesIntent');
    },

    'YesIntent': function() {
      getData(callback(title) {
        this.response.speak('Here are your data ' + title);
        this.emit(':responseReady');
      }),
    };

function getData() {
  var ddb = new AWS.DynamoDB.DocumentClient({
    region: 'us-west-1'
  });
  var params = {
    TableName: 'WallyFlow_StartTime',
    Key: 'TimeStamp',
  };
  ddb.get(params, function(err, data) {
    if (err) {
      callback(err, null);
    } else {
      title = data.Item.title;
    }
  });
}

Sorry, in this style you need more braces :) Updated to:

'YesIntent': function () {
    getData( {
    callback(title) {
                this.response.speak('Here are your data ' + title); 
        this.emit(':responseReady'); 
    }})
}};

I suspect it should be something like this. callback should be the name of the parameter to the getData() function, not something you call in the argument. The argument to getData() should be a function.

And getData() should call the callback function in the non-error case as well as the error case.

You also need an extra } to end the handlers object, and the end of the statement that calls getData() should be ; , not , .

 const handlers = { 'LaunchRequest': function() { this.emit('YesIntent'); }, 'YesIntent': function() { getData(function(title) { this.response.speak('Here are your data ' + title); this.emit(':responseReady'); }); } }; function getData(callback) { var ddb = new AWS.DynamoDB.DocumentClient({ region: 'us-west-1' }); var params = { TableName: 'WallyFlow_StartTime', Key: 'TimeStamp', }; ddb.get(params, function(err, data) { if (err) { callback(err, null); } else { title = data.Item.title; callback(title); } }); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM