I have following code:
export class GetPersonsService {
url="https://swapi.co/api/people/";
personsObservables;
headers: HttpHeaders = new HttpHeaders()
.set('Accept', 'application/json');
constructor(private http:HttpClient) { }
getPerson(personIndex){
return this.http.get<Person>(`${this.url}${personIndex}`,{headers:this.headers});
}
getPersons(){
for(let i=0;i<10;i++){
this.personsObservables.push(this.getPerson(i));
}
console.log(this.personsObservables);
return forkJoin(this.personsObservables);
}
}
and I don't understand why this.personsObservables is undefined because If I invoke getPerson(1) I am getting observable.And the next question how to maintain api if there are 10/100 or more items and I want to detect how many are them.
subscribe. You want to do asynchronous data as synchronously.personsObservables = [];it'll remove undefined errorgetPersons()method you have to do something like:getPersons().subscribe()