0
votes

i have a problem with my Alexa Skill. The code is like this:

 var options = { method: 'GET',
      url: 'http://98f8cd20.ngrok.io/products',
      headers: 
       { 'Postman-Token': 'f4e1b171-aae5-46d5-baeb-7903978cf10c',
         'cache-control': 'no-cache',
         'Content-Type': 'application/json' } };


    const callExternalApi = (callback) => {
     request(options, (error, response, body) => {
      if (error) {
          return callback(error)
      }
            const data = JSON.parse(body);
            return callback(data)

    })
    } module.exports.callApi = callExternalApi

and

apicaller.callApi(function(antwort){
     var test = antwort;

    console.log(test)
})

When i test it in my sublime editor there is no problem at all but when i test it with my alexa Skill i get an error. The code over there looks like this:

 'AllCarsIntent': function () {

        apicaller.callApi(function(antwort){
         var test = antwort.count;

          this.response.speak(test).listen("Tell me what you think is the world's most popular sport.")
          this.emit(':responseReady')


         //.listen("Tell me what you think is the world's most popular sport.")
         //this.emit(':responseReady')
     });

I think there is a problem with "this" in my callback. This is how my error looks like:

START RequestId: 4064b501-e1b2-11e8-b227-bb50cdd263c7 Version: $LATEST 2018-11-06T10:53:53.718Z 4064b501-e1b2-11e8-b227-bb50cdd263c7 Warning: Application ID is not set 2018-11-06T10:53:54.118Z 4064b501-e1b2-11e8-b227-bb50cdd263c7 TypeError: Cannot read property 'response' of undefined at /var/task/index.js:29:12 at Request.request [as _callback] (/var/task/data.js:19:12) at Request.self.callback (/var/task/node_modules/request/request.js:185:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (/var/task/node_modules/request/request.js:1161:10) at emitOne (events.js:116:13) at Request.emit (events.js:211:7) at IncomingMessage. (/var/task/node_modules/request/request.js:1083:12) at Object.onceWrapper (events.js:313:30) END RequestId: 4064b501-e1b2-11e8-b227-bb50cdd263c7 REPORT RequestId: 4064b501-e1b2-11e8-b227-bb50cdd263c7 Duration: 440.67 ms Billed Duration: 500 ms Memory Size: 128 MB Max Memory Used: 44 MB
RequestId: 4064b501-e1b2-11e8-b227-bb50cdd263c7 Process exited before completing request

Anyone has an idea what my mistake is? I would really appreciate your help! Thank you in advance!

1

1 Answers

0
votes

The context is lost once inside the API call function. this does not refer anymore to the outer context. Define the variable test outside the callApi function then assign the value inside the calllApi function, and then do the speak and emit also outside the callApi function. You'll have to wait for the data of the API call to come back so you need to use async/await and node v8+ Here's an example of such API call but using the latest version of the ASK SDK (please migrate to this one, the one you're using is deprecated).