newuser-a2e46df7-77e0-4383-b2ba-2491d1c80deb avatar image

HTTP request callback not being fired

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) {
  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');
      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 =[gardenNumber - 1].id;
        } else {
          console.log("parsing error");
    } else {
      console.log("failed request");
  }); } catch(e) {
  req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
  req.on('finish', () => {
  console.log("req done");
var handlers = {
'SelectGardenIntent': function () {
      //var filledSlots =;
      var gardenNumber = this.event.request.intent.slots.Garden.value;
      user_id = this.event.session.user.accessToken;
      this.emit(':tell', `OK, garden ${gardenNumber} selected, user id is ${user_id}, garden id is ${garden_id}, device id is ${dev_id}`);
alexa skills kitlambdaintentsnode_js
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Nick Gardner avatar image
Nick Gardner answered

It looks like this is probably a callback ordering issue. You're not waiting for the callback to return before calling req.end(), which will prematurely end the request. If you restructure your callbacks so the end call only happens on an error or when the request is finished it should work.

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.