0

So I am using a getAll to get all of a school's Virtual Hosts, which comes back as a list of ID's (id's of the Virtual Hosts). Then, in order to get the data of the Virtual Hosts, I must call another get on each of the ID's. I do this through a for loop like so--

 this.apiService.findAll("virtualhosts/deltest") .subscribe((data) => { this.data = data.docs[0] error => console.log(error) for (let i = 0; i < this.data.VirtualHosts.length; i++) { this.apiService.findVH(this.data.VirtualHosts[i]) .subscribe((data) => { this.data = data.docs[0] this.jsonData = this.jsonData.concat(this.data) }) } }); 

The problem is, the apiService.findVH does not call the get in order of the ID's given by the apiService.findAll. Every time it gets called, it doesn't compute the ID's in order of the list (hence the indices of this.data.VirtualHosts[i] is different) How can I make the findVH (get call) call the indices in order?

1 Answer 1

2

You can map an observable:

this .apiService .findAll("virtualhosts/deltest") .flatMap(data => this.apiService.findVH(data.VirtualHosts)) .subscribe(/* whatever you want */) ; 
Sign up to request clarification or add additional context in comments.

14 Comments

map or flatMap?
You can use both, but flatMap is definitely better.
then change your answer
how do i incorporate having to do a for loop through apiService.findVH into this?
you don't have to loop anything, Observable is an Iterable Object. Just look at my example.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.