-2

The getDetails() method returns an Details[] and with that array and there is a name property which I am passing to another http call

Details {
    name: String;

Details

 getDetails()
     return this.https.get("/someValue") //this return Details[] from here I have to take  name property

return

{name : hello}

Information

 getInformations(name:string) {
    return this.https.get("/anotherValue"+"?"+ name)
 }

I am trying to first call getDetails() and pass the name property to

Information{
  Hieght:string
  age:string
}

getInformations()
this.getDetails().pipe(mergeMap(x => 
 this.getInformations(x.name)).subscribe((inforamtionResults:Information[])=>{
  console.log("Checking Informations"+inforamtionResults.length) 

return

 {height:160,
   age: 23
   }

This is not working as x value is an array. How do iterate the array and pass the array property as parameter?

Subham
  • 420
  • 3
  • 11
  • 34

2 Answers2

0

As you said, you need to iterate each this.getInformations(x.id) Details. You can do this by simply using Array.map(). However, mergeMap wants one observable, but what we have is an array of observables. Thankfully, we can combine those array of observable to just one observable using rxjs' forkJoin.

this.getDetails()
  .pipe(
    mergeMap((details) =>
      forkJoin(details.map((x) => this.getInformations(x.id)))
    )
  )
  .subscribe((x) => console.log(x));

Here is a sample demo. I've used a different API but the implementation is similar.

Blackraspberryyy
  • 2,016
  • 2
  • 11
  • 22
  • When dealing with rxjs, this [operator decision tree](https://rxjs-dev.firebaseapp.com/operator-decision-tree) helps me a lot so I'll just put this here. – Blackraspberryyy Sep 13 '20 at 12:35
  • For x I am getting double array vaue x[][] – Subham Sep 13 '20 at 12:46
  • `forkJoin` isn't deprecated as said [here](https://rxjs-dev.firebaseapp.com/api/index/function/forkJoin) (I am using `rxjs 6.5.4`). Can you update your question to also show the structure of your data that you'll receive from `getDetails()` and `getInformations()`? – Blackraspberryyy Sep 13 '20 at 12:57
  • I have updated the question...Can you please have a look.I dont want to iterate each this.getInformations(x.id).Just want to do a sequence HTTP calls first retriev get details and take the name propert from there and then pass the same property to the another call getInformation(x.id) – Subham Sep 13 '20 at 13:05
  • Is `getDetails()` really returning an array? But in your question, you said that it returns `{name: "hello"}` which is clearly not an array. Also, `Details` does not have an `id` property, but you tried to access it here: `this.getInformations(x.id)`. I am more confused now. Can you create a [fiddle](https://jsfiddle.net/), or a working demo at [codesandbox](https://codesandbox.io/) so we can help you better? – Blackraspberryyy Sep 13 '20 at 13:30
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221415/discussion-between-subham-and-blackraspberryyy). – Subham Sep 13 '20 at 13:34
-1

You need to try Like this.

 getDetails() {
    return this.http.get("/someValue")
      .pipe(
        mergeMap((x) => {
          return this.getInformations(x.id)
        })
      );
  }
Prakash Harvani
  • 1,007
  • 7
  • 18