-1

I am making a skill for the Amazon Echo. In my handlers, I have an intent (SelectGardenIntent) that obtains the user_id (needed for following HTTP requests) from the access token successfully, as well as a variable called gardenNumber which is a slot value. To complete the request, I need two other values, the garden_id and the dev_id. I use this gardenNumber and pass it into a function called getGardenId, which will assign the one of the data from the HTTP request to the variable garden_id I have defined in index.js. There are no issues with user_id and gardenNumber. When the function is run, there are no errors from the request, but the callback function with the response is also not executed. The user_id, "about to enter request", and "req done" are correctly logged when tested, but the other log statements in the callback function are not since it is not run. The result is that garden_id is undefined. dev_id is obtained in another method that depends on this garden_id, so dev_id is also undefined. Please help me on this issue. I have pasted the relevant code below.

...
var user_id, garden_id, dev_id;
...
function getGardenId (gardenNumber) {
  console.log(user_id);
  var path = '/api/rest/client/getgardeninfo?&userid=' + user_id;
  var options = {
    hostname: server_ip,
    port: 80,
    path: path,
    method: 'GET'
  }
  console.log("about to enter request");
  var req = http.request(options, (res) => {
    console.log('entered request');
    if (res.statusCode === 200) {
      console.log('successful request');
      res.setEncoding('utf8');
      var body = "";
      res.on('data', (chunk) => {
        console.log('adding data');
        body += chunk.toString();
      });
      res.on('end', () => {
        var obj = JSON.parse(body);
        console.log('successfully parsed');
        if (obj.error === 200) {
          console.log('##gardenid successfully obtained');
          garden_id = obj.data[gardenNumber - 1].id;
        } else {
          console.log("parsing error");
        }
      });
    } else {
      console.log("failed request");
    }
  }); } catch(e) {
    console.log("ERROR");
  }
  req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
  });
  req.on('finish', () => {
    console.log('ended');
  })
  req.end();
  console.log("req done");
}
...
var handlers = {
...
'SelectGardenIntent': function () {
      //var filledSlots = delegateSlotCollection.call(this);
      var gardenNumber = this.event.request.intent.slots.Garden.value;
      user_id = this.event.session.user.accessToken;
      getGardenId(gardenNumber);
      getDevId(garden_id);
      this.emit(':tell', `OK, garden ${gardenNumber} selected, user id is ${user_id}, garden id is ${garden_id}, device id is ${dev_id}`);
 }
...
}
Michael Xue
  • 21
  • 1
  • 3

1 Answers1

-1

You'd better use npm request to make calls.

request.get({
    url: 'http://' + server_ip + '/api/rest/client/getgardeninfo?&userid=' + user_id
}, function (err, res, body) {
    console.log(body);
})
emil
  • 6,074
  • 4
  • 30
  • 38
  • While `request` seems to be a nice little library, `http.request` should totally work. – Zach Lysobey Jul 29 '17 at 00:34
  • There does not seem to be any improvement. The request is still not working. – Michael Xue Jul 29 '17 at 00:42
  • Please post full url you are making request to @MichaelXue – emil Jul 29 '17 at 00:44
  • The server_ip is "www.rainconn.com" and a sample user_id is 1037. – Michael Xue Jul 29 '17 at 00:52
  • the url seems valid. Did you install request library? – emil Jul 29 '17 at 00:55
  • Yes, I did. My results have not changed. I do not think request would be different from http.request as Zach Lysobey mentioned. – Michael Xue Jul 29 '17 at 00:57
  • what do you mean by result not change? You are not getting console output at all? – emil Jul 29 '17 at 01:00
  • Is it possible there is some conflict with req.end() and how long it takes for the data to be stored in the response object? I have seen some people having similar issues as me who have implemented additional callback functions, but I do not quite understand what is going on in this solution. Is this a possibility? – Michael Xue Jul 29 '17 at 01:06
  • Ok, I figured out the issue. I had my Alexa emit function after these getId functions, but due to the asynchronous nature of the http request, the data was not coming in before Alexa was emitting speech. And once that happened, the entire function finished, so the id's were never assigned. – Michael Xue Jul 29 '17 at 01:44