提供' undefined'预计流量在哪里

时间:2018-04-10 22:01:36

标签: angular typescript rxjs

我正在尝试使用Rxjs发送链式http请求,但我收到此错误...

  

错误:未捕获(在承诺中):TypeError:您提供了未定义的'预期流的地方。您可以提供Observable,Promise,Array或Iterable。   TypeError:您提供了' undefined'预期流的地方。您可以提供Observable,Promise,Array或Iterable。

我想从我的API获取位置对象,然后我希望根据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);
    })  
  }
}

注意:非常奇怪的是,在此错误之后,控制台打印纬度和经度正确! (&#39; // PRINTS CORRECT&#39;评论)

编辑:是的,我错了,我在if中声明了变量,但最后这不是问题。很快发布解决方案。

1 个答案:

答案 0 :(得分:1)

我解决了......问题是geocoder.geocode()函数没有返回值,而mergeMap()期待PromiseObservable等,geocoder.geocode()返回undefined。我的解决方案是使用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);
            })  
          }