3

the variables have all been declared judiciously, how can i pass the value gotten from the first observable function which is this.acNum as the parameter to resolve the second observable function as shown in ngOnInit, when i do this, it seems the value of this.acNum is actually empty but it shows up in my component html template.

ngOnInit() {   
    this.getFQ(8030495); 
    this.getHS(this.acNum);  
}

getFormQ(formqid){
    this.getFQService.GetFQ(fqid).subscribe({
        next: fQDetails => {
            this.fQDetails = fQDetails;
            //populating app details
            this.acNum = this.fQDetails["data"]["data1"];
            this.appName = this.fQDetails["data"]["data1"];
        },
        error: err=> {
            this.errorMessage = err;
        }
    })
};

getHSC(hsc){
    this.gethscservice.GetHsC(hsc).subscribe({
        next:HScResp=>{ this.HScResp = HScResp;
            console.log(HScResp["data"]);
        },
    });
};

thanks for the help.

C.OG
  • 6,236
  • 3
  • 20
  • 38
darkel
  • 31
  • 2
  • 2
    this is **asynchronous** please refer first to: https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular and https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call before moving on. After you get the hang of that, please look into chaining requests with rxjs `switchMap` or `mergeMap`. – AT82 Nov 18 '19 at 12:44

3 Answers3

3

Try like this:

this.getFQService.GetFQ(fqid).subscribe(res1:any => {
    this.acNum = res1["data"]["data1"];
    this.appName = res1["data"]["data1"];
    this.gethscservice.GetHsC(this.acNum).subscribe(res2 => {
  });
});

or you can use mergeMap

var result = this.getFQService.GetFQ(fqid).pipe(
  mergeMap(res1 => this.gethscservice.GetHsC(res1["data"]["data1"]))
);
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
1

You can try to call your getHSC function in your getFormQ function when you have set the value for this.acNum or have a look at the comment of @AJT82.

JuNe
  • 1,911
  • 9
  • 28
0

Try forkJoin with mergeMap:

ngOnInit() {   
 this.getFQ(8030495); 
 var first = this.acNum();
 var second = this.getHS();

 const result = first.pipe(mergeMap(q => forkJoin(second(q)));
 }
Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37