Member Varible undefined

7 views
Skip to first unread message

oluwaseun paul

unread,
Dec 13, 2017, 7:02:22 AM12/13/17
to Angular and AngularJS discussion
I have this code below in my angular app :

fetchAllRiders(){

this.http.get( this.API + '/driver/all').map( res => res.json())
.subscribe( data => data.map( rider =>{
var distances = [];
this.geocoder.geocode({location: {lat: rider.driver_lat, lng: rider.driver_lng}}, async function(results, status){

if( status == google.maps.GeocoderStatus.OK){
//then find distance matrix of each rider
this.distanceMatrixService.getDistanceMatrix({
origins: [this.from],
destinations: [results[0].formatted_address],
travelMode: google.maps.TravelMode.DRIVING
}, async function(response, status ){
if( status == google.maps.GeocoderStatus.OK){
await distances.push(response.rows[0].elements[0].distance.text );
}
} )
console.log( distances );
}
})
} )
//looping ends
);
}

but i keep getting errors :
this.distanceMatrixService is undefined meanwhile its defined in my constructor.
please what am i doing wrong. thanks

Sander Elias

unread,
Dec 13, 2017, 11:40:54 AM12/13/17
to Angular and AngularJS discussion
Hi Oluwasuen,

Probably you are using some async code to set the value. The error you show is probably some part of your code is trying to use the value, before the async functions are done.
This is similar to what happens:

let x = 1
setTimeout
(() => x=2,1000)
console
.log(x) // log's 1 as the x=2 will run ~1000ms from now.


Regards
Sander
Reply all
Reply to author
Forward
0 new messages