Search code examples
javascriptnode.jsamazon-web-servicesaws-lambdaalexa-skills-kit

How do you define the emit() property in a Lambda Function


I'm simply trying to get Alexa to say the data that's in my google spread sheet. I'm using the google-spreatsheet node.js module instead of http.get() method.

Here is my intent function

'GetPainterIntent': function() {
        // spreadsheet key is the long id in the sheets URL 
var doc = new GoogleSpreadsheet('1K-dl08g8s27TgF5yMGv_Q5tBbkpRpGWrt3RkUB2mVKk');
var sheet;

async.series([
  function setAuth(step) {
    // see notes below for authentication instructions! 
    var creds = require('./creds.json');
    // OR, if you cannot save the file locally (like on heroku) 
    /*
    var creds_json = {
      client_email: ' ',
      private_key: ' '
    }
        */
    doc.useServiceAccountAuth(creds, step);

  },
  function getInfoAndWorksheets(step) {

    doc.getInfo(function(err, info) {
      console.log('Loaded doc: '+info.title+' by '+info.author.email);
      sheet = info.worksheets[0];

      //console.log('sheet 1: '+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
      this.emit('ask:', 'Do you want'+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
      step();
    });

  }

], function(err){
    if( err ) {
      console.log('Error: '+err);
    }
});

    } 

In the Execution result: failed logs, One of the key things I get is this...

 {
 "errorMessage": "RequestId: 79ed1bc9-aaee-11e7-878b-5fe3ebd777ae 
 Process 
 exited before completing request"
 }

TypeError: Cannot read property 'emit' of undefined
at getInfoAndWorksheets (/var/task/index.js:51:9)
at /var/task/node_modules/async/dist/async.js:3853:24
at replenish (/var/task/node_modules/async/dist/async.js:946:17)
at iterateeCallback

Is the properties inside emit is undefined? Or How do you define the property of emit or did I read that wrong?


Solution

  • I think the problem is that you lost your this the way you coded it, maybe because its using async series. Save this at the top with a line like var self=this then use self.emit instead.