首页 文章

提供了'undefined'预期流的位置

提问于
浏览
0

我试图使用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);
    })  
  }
}

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

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

1 回答

  • 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);
                })  
              }
    

相关问题