I am currently having trouble accessing a value within a function but this is OUTSIDE OF A PROMISE.
Here is my code:
service.getDistanceMatrix({
origins: ["Waterloo ON"],
destinations: destination,
travelMode: 'DRIVING',
avoidHighways: false,
avoidTolls: false
}, function(response, status){
if (status == 'OK') {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
// _this.
// console.log(distance);
location.distFinal = distance;
location.setState({
dist: distance
})
// return distance;
// return distance;
// console.dir(distance)
// tryA = distance;
}
}
}
})
console.dir(location);
console.dir(location.distFinal);
I am trying to access the distance so I did console.dir(location.distFinal) but it gave me an undefined value. However, when I did console.dir(location), it gave me the object with distFinal as a value....
This is what I mean:
Line 126 is the console.dir(location) and line 127is console.dir(location.distFinal)
Please! I just want to be able to extract the distance!
