I have an Alexa skill that takes the device's address and uses that information to find a nearby facility.
I have taken a look at the solution here as proposed by @jfriend00. My callbacks are structured the same way. I still don't understand why my callbacks aren't returning before the rest of the code runs. I should be seeing the console.info()
call from the callback of getJSON()
, but it never runs.
EDIT: I clearly don't understand how this async stuff works as my logs are completely out of whack. Can someone please explain to me what is going on? I looked at the solution I linked and my code looks as similar as possible to his barebones solutions. I don't understand where I'm going wrong.
Console logs
getKaiserBuildingHandler()
const getKaiserBuildingHandler = function() {
console.info("Starting getKaiserBuildingHandler()");
...
switch(addressResponse.statusCode) {
case 200:
console.log("Address successfully retrieved, now responding to user.");
const addressObj = addressResponse.address;
const address = `${addressObj['addressLine1']}, ${addressObj['city']}, ${addressObj['stateOrRegion']} ${addressObj['postalCode']}`;
var ADDRESS_MESSAGE = Messages.ADDRESS_AVAILABLE;
const alexa = this;
getKaiserBuildingHelper(buildingType, address, function(response) {
ADDRESS_MESSAGE += response;
alexa.emit(":tell", ADDRESS_MESSAGE);
console.info("Ending getKaiserBuildingHandler()");
});
break;
...
}
});
...
};
getKaiserBuildingHelper()
const getKaiserBuildingHelper = function(buildingType, address, callback) {
console.info("Starting getKaiserBuildingHelper()");
// var facilityOutput = Messages.ERROR;
var facilityOutput = "Inside building helper function, initial value.";
if (buildingType == BLDG_TYPE.PHARMACY ||
buildingType == BLDG_TYPE.CLINIC ||
buildingType == BLDG_TYPE.HOSPITAL) {
...
facilityOutput = "Before get JSON call.";
getJSON(buildingType, function(err, data) {
if (data != "ERROR") {
console.info("Received data from callback: ", data);
facilityOutput = "The closest Kaiser Permanente " + buildingType + " to your location is located at " + data + ".";
} else {
console.error("Error with data received from callback. ", err);
facilityOutput = "Entered JSON call, returned ERROR.";
}
});
}
...
callback(facilityOutput);
console.info("Ending getKaiserBuildingHelper()");
}
getJSON()
const getJSON = function(building, callback) {
console.info("Starting getJSON()");
Axios
.get(getFacilitySearchEndpoint(building))
.then(function(response) {
const responseData = response.data.query.search[0].title;
callback(null, responseData);
console.info("Data retrieved from Axios call");
console.info("Ending getJSON()");
})
.catch(function(error) {
callback(error, "ERROR");
console.info("Error caught from Axios call");
console.info("Ending getJSON()");
});
}
getFacilitySearchEndpoint() [wikipedia api just as placeholders]
const getFacilitySearchEndpoint = function(building) {
console.info("Starting getFacilitySearchEndpoint()");
switch (building) {
case BLDG_TYPE.HOSPITAL:
console.info("Ending getFacilitySearchEndpoint() with " + building);
return "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&utf8=1&srsearch=Albert+Einstein";
break;
case BLDG_TYPE.PHARMACY:
console.info("Ending getFacilitySearchEndpoint() with " + building);
return "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&utf8=1&srsearch=Harry+Potter";
break;
case BLDG_TYPE.CLINIC:
console.info("Ending getFacilitySearchEndpoint() with " + building);
return "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&utf8=1&srsearch=Tony+Stark";
break;
default:
console.info("Ending getFacilitySearchEndpoint() with default");
break;
}
console.info("Ending getFacilitySearchEndpoint()");
}