Reputation: 2014
I am trying to send chained http requests using Rxjs, but I am getting this error...
Error: Uncaught (in promise): TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
I want to get location object from my API, then I want to get latitude and longitude based on location.address
.
declare const require : any;
@Injectable()
export class GoogleMapLocationResolver implements Resolve<{location: Location, longitude: number, latitude: number }> {
constructor( private locationService: LocationService,
private route: ActivatedRoute,
private router: Router){}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): {location: Location, longitude: number, latitude: number } | Observable<{location: Location, longitude:number, latitude: number }> | Promise<{location: Location, longitude: number, latitude: number }> {
let geocoder = require('geocoder');
return this.locationService.getLocation(route.params['id']).map(
(response: Response)=> { return response.json() }
).mergeMap(location => geocoder.geocode(location.address, function(err, data){
let latitude
let longitude
if(data.status === 'OK'){
console.log('Status ok: ')
console.log(data)
let results = data.results;
latitude = results[0].geometry.location.lat;
longitude = results[0].geometry.location.lng;
console.log(latitude); // PRINTS CORRECT
console.log(longitude); // PRINTS CORRECT
}
return {location, longitude, latitude};
})).catch(error => {
this.router.navigate(['/not-found'])
return Observable.throw(error);
})
}
}
NOTE: Whats very weird is that after this error, console prints latitude and longitude correct! ('// PRINTS CORRECT' comment)
EDIT: Yep my mistake, I declared variables in if, but that was not a problem at the end. Posting solution soon.
Upvotes: 0
Views: 19964
Reputation: 2014
I solved it... Problem is that geocoder.geocode()
function has no return value, while mergeMap()
is expecting an Promise
,Observable
etc, geocoder.geocode()
returned undefined
. My solution is to wrap this function with Promise
.
declare const require : any;
@Injectable()
export class GoogleMapLocationResolver implements Resolve<{location: Location, longitude: number, latitude: number }> {
constructor( private locationService: LocationService,
private route: ActivatedRoute,
private router: Router){}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): {location: Location, longitude: number, latitude: number } | Observable<{location: Location, longitude:number, latitude: number }> | Promise<{location: Location, longitude: number, latitude: number }> {
let geocoder = require('geocoder');
return this.locationService.getLocation(route.params['id']).map(
(response: Response)=> { return response.json() }
).mergeMap( location => new Promise<any>((resolve, reject) => {geocoder.geocode(location.address, function(err, data){
let latitude
let longitude
if(data.status === 'OK'){
console.log('Status ok: ')
console.log(data)
let results = data.results;
latitude = results[0].geometry.location.lat;
longitude = results[0].geometry.location.lng;
console.log(latitude);
console.log(longitude);
}
resolve({location, longitude, latitude}(;
})
})).catch(error => {
this.router.navigate(['/not-found'])
return Observable.throw(error);
})
}
Upvotes: 1