I've been stuck on this for a few days and I know that there is an easy answer and its something that I'm overlookng. Console.log is synchronous and I'm able to get the return value from Googles Distance API, but when I try and set req.body["OTHER nRow1"]= I get a cannot ready property toString of undefined (in a pdftk (pdffiller npm)). So, I think I'm still returning the promise. How do I set req.body to the results of the axios get? I've gone down the rabbit hole and I've tried everything. Thank you.
fetchDistance = async function(DISTANCE, req) {
return axios
.get(DISTANCE)
.then(response => {
console.log();
const miles = response.data.rows[0].elements[0].distance.text
.replace("mi", "")
.trim();
return miles;
})
.catch(function(error) {
console.log(error);
})
.then(result => {
console.log("toLoc: " + result);
req["OTHER nRow1"] = result;
return result;
});
};
var AirportMiles;
fetchDistance(DISTANCE, req.body)
.then(function(result) {
console.log(DISTANCE + "DISTANCE!!!");
console.log(result + "RESULT!!!");
console.log((req.body["OTHER nRow1"] = result));
return result;
})
.catch(error => {
console.log(error);
})
.then(data => {
console.log(data + "DATA!!!!!");
getData(data);
return (req.body["OTHER nRow1"] = data);
});
console.log("REQ: " + req.body["OTHER nRow1"]);
req.body["Text13"] = perdiem[0].mileage.MileageCost;
}
var mi;
const getData = data => {
mi = toInt(data);
};
req.body["OTHER nRow1"] = mi;
// Mileage to Form END