joshua1991
joshua1991

Reputation: 129

angular2- Observable getting data out of order

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?

Upvotes: 0

Views: 261

Answers (1)

Hitmands
Hitmands

Reputation: 14189

You can map an observable:

this
  .apiService 
  .findAll("virtualhosts/deltest")
  .flatMap(data => this.apiService.findVH(data.VirtualHosts))
  .subscribe(/* whatever you want */)
;

Upvotes: 2

Related Questions