typescript 在预期流的地方提供了“未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49763492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:21:00  来源:igfitidea点击:

Provided 'undefined' where stream was expected

angulartypescriptrxjs

提问by curious95

I am trying to send chained http requests using Rxjs, but I am getting this error...

我正在尝试使用 Rxjs 发送链接的 http 请求,但出现此错误...

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.

错误:未捕获(承诺):类型错误:您在需要流的地方提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。类型错误:您在需要流的地方提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。

I want to get location object from my API, then I want to get latitude and longitude based on location.address.

我想从我的 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: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.

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

回答by curious95

I solved it... Problem is that geocoder.geocode()function has no return value, while mergeMap()is expecting an Promise,Observableetc, geocoder.geocode()returned undefined. My solution is to wrap this function with Promise.

我解决它......问题是,geocoder.geocode()函数没有返回值,而mergeMap()期待的PromiseObservablegeocoder.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);
            })  
          }