简体   繁体   中英

How to enforce order http requests on a list of observables in Angular 7

I need to get a list of Id's ids => this.getIds() and get their names => getNames(id) for each id in the list of ids using mergeMap(). Can someone please check what I'm doing wrong below (it fails to compile)?

in, myservice.ts:

getIds()<any> {
     this.http
        .post(url1, '')
        .subscribe(map(({ Ids }: any) => Ids.map(item => ({Id: item.Id, Name: item.Name }))));
    }
getNames():Observable<any[]> {
     return this.http
        .post(url2, '')
        .pipe(map(({ Names }: any) => Names.map(item => ({Sid: item.Id, Name: item.Name }))));
}

in, component.ts:

  getIds(): void {
  this.myservice
     .getIds.pipe(
     mergeMap(id => this.getNames(id)),
     mergeMap(names => names),
     toArray()
     )
  }
getNames(): void {
   this.myservice
     .getNames(ids)
     .subscribe(val => this.data = val);
  }

You're not returning anything from getNames method of component, which should ideally return an Observable

getNames(): void {
   return this.myservice
      .getNames(ids)
}

You need to change the signature of your getIds function from:

getIds()<any> 

to

getIds(): any

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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