0

I am creating a Alexa skill (Lambda function) node.js - And I am having having an issue passing in the "zipcode" const to the getLocation callback function, if i output the zipcode it will work.

The getLocation function it's not returning anything, and I am guessing it's because the zipcode param is not being passed in properly into the function.

There is nothing wrong with the function because if i replace the

var url = "localhost/api.php?zip="+zipcode;

with

var url = "localhost/api.php?zip=41242"; or any zip code it works.

What am i doing wrong?

let zipcode = request.intent.slots.zipcode.value;

            getLocation(function(location, err) {
            if(err) {
                context.fail(err);
            } else {
                options.speechText += location.distance + " miles away, 
                You can get there by going to " + location.street_address + " in " + location.city + ", " + location.state;
                options.endSession = true;
                context.succeed(buildResponse(options));
            }
        });

function getLocation(callback, zipcode) {
var url = "localhost/api.php?zip="+zipcode;

var req = http.get(url, function(res) {
  var body = "";

  res.on('data', function(chunk) {
    body += chunk;
  });

  res.on('end', function() {
    body = body.replace(/\\/g, '');
    var location = JSON.parse(body);
    callback(location);
  });

  res.on('error', function(err) {
    callback('', err);
  });

}); }

koepitz
  • 5
  • 3
  • When you call *getLocation* you only pass one argument - the callback - so the *zipcode* argument will always be *undefined*. – Julian Goacher Jun 13 '17 at 21:31
  • @JulianGoacher how should i approach this then? – koepitz Jun 13 '17 at 21:34
  • Well, if the zip code is something like "abc123" then you want to change the function call to `getLocation(function(location, err) { /*... callback code ... */, "abc123")` - i.e. include it as the second argument in the function call. – Julian Goacher Jun 13 '17 at 21:37
  • That worked, thanks! @JulianGoacher – koepitz Jun 13 '17 at 21:42

1 Answers1

0

In the getLocation() function you should pass the zipcode as the second argument . your code will be something like this:

 getLocation(function(location, err) {
       */you code here /*
    } , zipcode);
Wejd DAGHFOUS
  • 1,110
  • 7
  • 13