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);
});
}); }