I am trying to make 2 api calls where one api gets the array of IDs that I needed to make another api call according to the IDs that were returned with some object where I can manipulate into displaying some form inputs according to the data I received from it.
But I am now stuck while attempting to make the second call. Any advice on how I could make the second call would be appreciated.
//api.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ApiService {
baseurl = 'http://localhost:5000';
constructor(private http: HttpClient) { }
getIds(id){
return this.http.get(this.baseurl + '/types/' + id);
}
getCustomFieldsById(id){
return this.http.get(this.baseurl + '/types/fields/' + id );
}
}
//custom.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { ApiService } from '../api.service';
import { map, mergeMap, switchMap } from 'rxjs/operators';
@Component({
selector: 'app-custom',
templateUrl: './custom.component.html'
})
export class CustomComponent implements OnInit {
constructor(private apiService: ApiService) {
}
ngOnInit(){
}
getCustomFields(id){
this.apiService.getIds(id)
//this returns an obj in which arrayData constains an array of ids that is needs to be passed into the other api call
.pipe(
map((obj: any) => {
//trying to map the array from the obj
const data = obj.arrayData;
return data;
}),
mergeMap(data =>
//data here is an array of ids
//suppose to receive the ids from the arrayData above
this.apiService.getCustomFieldsById(data)
)
).subscribe(response => {
console.log(response);
});
}
}