简体   繁体   English

传入 RXJS 中的 switchMap() 时对象数组的奇怪行为

[英]Strange behaviour of an array of objects when passed into switchMap() in RXJS

I have a simple goal of creating an observable that emits a list of all the countries in the world.我有一个简单的目标,即创建一个可输出世界上所有国家/地区列表的 observable。 The type of the observable is Country[] , ie the store value is typed as an array of objects. observable 的类型是Country[] ,即存储值的类型是对象数组。 However, when passed to switchMap , it magically gets converted into type Country .但是,当传递给switchMap ,它会神奇地转换为Country类型。 I have no idea what causes this behavior and if it is not, by chance, on my side.我不知道是什么导致了这种行为,如果不是偶然的,我也不知道。 Here is the function in question:这是有问题的函数:

public getAllCountries(): Observable<Country[]> {

      const getAppState = createFeatureSelector<ServicesState>('services');
      const getCountries = createSelector( getAppState, (state: ServicesState): Country[] => state.countries);
      // as you can see, this is the NGRX store slice that must be of type Observable<Country[]>
      const countriesFromStore$ = this.store.pipe(select(getCountries));

      return countriesFromStore$.pipe(
        // this is the switchMap that causes the error
        switchMap( countries => {
        // this is the return statement that somehow converts an array of objects into a single object??
        if (countries)  { return countries; }
        const countriesFromServer$: Observable<Country[]> = this.http.get<FilteredArrayResponse<Country>>
           (this.baseUrl, this.config.httpGetJsonOptions).pipe(
               tap( result => this.store.dispatch( new LoadCountriesAction(result.data)) ),
               map( result => result.data)
           );
        return countriesFromServer$; })
    );
  }

In case you have questions :如果您有疑问:

  • FilteredArrayResponse is a generic interface with data attribute actually containing the array FilteredArrayResponse是一个通用接口,其data属性实际上包含数组
  • The full error message is Type 'Observable<Country | Country[]>' is not assignable to type 'Observable<Country[]>'.完整的错误消息是Type 'Observable<Country | Country[]>' is not assignable to type 'Observable<Country[]>'. Type 'Observable<Country | Country[]>' is not assignable to type 'Observable<Country[]>'.
  • My guess is that somehow switchMap confuses an Observable of Arrays with an Array of Observables...我的猜测是,不知何故switchMap将可观察的数组与可观察的数组混淆了......
  • The return type of result.data is always an array result.data的返回类型始终是一个数组
  • Other operators from the map family exhibit the same behaviour map系列中的其他运算符表现出相同的行为

Try returning of(countries) in the switchMap ( import { of } from 'rxjs'; ).尝试在switchMap返回of(countries)import { of } from 'rxjs'; )。 I think that should fix it.我认为这应该解决它。 The switchMap needs to switch to an Observable . switchMap需要切换到Observable

import { of } from 'rxjs';
...
switchMap((countries: Country[]) => {
  if (countries) {
    return of(countries);
  }
  ....
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM