0

I am using 3 endpoints for my application, 1 of the endpoints depends on other of the endpoint's response, which is a list, then I need to use each item of the list in order to use the other endpoint, let's call them epA, epB and epC, epA returns a list and then i use this list on epB, something like epA.Foreach( x => epB(x)) and I am trying to combine epB and epC into one combined list, since both share similar fields.

My problem is, I am too new using angular and observables, I don't know if there is a way to combine these epB and epC results (without mentioning that currently, I subscribe to the observables and assign its values to other objects I need)...If someone could give me a hand it would be greatly appreciated. Sorry if this is too messy, I have little experience posting here and coding in angular.

here's some code I currently have... it is a bit ugly but it does the job

code

this._serverRequests.epA(this._Token).subscribe(x => {
    this.servers = x;
    x.forEach(server => 
      this._serverRequests.epB(server)
      .subscribe(info => {
         this.serverInfo = info;
         this.GridModel.data = info['States'];
         this.GridModel.data.forEach(se => {
             se.Start = this.formatValuesPipe.transform(se.Start, 'grid');
         });
         this.GridModel.data.map( o => {
            o.ServerUrl = server;
         });
       })
     );
},
   error => this.errMsg = <any>error
);

this._serverRequests.epC(this._Token).subscribe(lic => {
   this.licensesList = lic;
   this.licensesModel.data = this.licensesList.LicenseUsageList;
   this.licensesModel.data.forEach(li => {
      li.AcquisitionTime = this.formatValuesPipe.transform(li.AcquisitionTime, 'grid'); 
   });
});

I also tried forkjoin, but since epA returns a list, I don't know how to call each item inside of the forkjoin

4

1 回答 1

1

You can map the response from the first call into an array of calls then use combineLatest to give an array of all the responses.

this._serverRequests.epA(this._Token).pipe
  map(server => this._serverRequests.epB(server)),
).subscribe(requests => {
  combineLatest(requests).subscribe(results => {
    // You have an array of the multiple results here
  })
});
于 2019-09-19T03:30:56.207 回答