How to call two method sequential

212 views
Skip to first unread message

sandeep...@gmail.com

unread,
Oct 19, 2020, 5:35:49 AM10/19/20
to Angular and AngularJS discussion
Hi,

I would like to call an service in which I need a session value. I dont want customer to enter the login details to create the session rather on page load itself I would like to provide the details.
 
1)  var session =  getSession();  // rest call from backend

 2) var info = getInformation (session) ; //rest call from backend

 3) return infor to cleint

 I would like to know how I can do it with one call. It does not work for me.

Arnaud Deman

unread,
Oct 20, 2020, 5:40:12 AM10/20/20
to Angular and AngularJS discussion
Hello Sandeep,
Assuming the backend calls return Observables, you could do something like this (untested):
getSession (). pipe (
mergeMap (sessions => getInformation (session))
);

This would return an Observable of the same type as getInformation (session).

Regards,
Arnaud.

Sandeep Shukla

unread,
Oct 20, 2020, 1:28:15 PM10/20/20
to ang...@googlegroups.com
Hi Arnaud,
Thanks for your response.
I did not understand it completely. The moment I call method in subsequent request session becomes undefined. Here is the code I am attaching.

      sessionId : string = "";
     
   this.loginService.getSession(t)
      .subscribe(data => {
        this.result=JSON.parse(JSON.stringify(data));
         sessionId = this.result;
      }, error => {
        this.result = JSON.stringify(error).toString();
      }); 
  // I only want to return result from below method to consumer while session is created in each request. I cannot store sessionId from //getSession. I am not sure how to combine them 
     this.loginService.searchDocument(this.sessionId , bodymsg)
      .subscribe(data => {
        this.result=JSON.parse(JSON.stringify(data));     
      }, error => {
        this.result = JSON.stringify(error).toString();
      }); 



Regards
Sandeep Sjhukla

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/d2ffc870-f1cd-4f18-96ca-357bccb163ccn%40googlegroups.com.


--
Regards
Sandeep Shukla,
Mobile# +31647969951

Arnaud Deman

unread,
Oct 21, 2020, 8:17:53 AM10/21/20
to Angular and AngularJS discussion
Hi Sandeep,

The calls to the backend are actually returning Observables. This is asynchronous, so the second call is probably done before the first Observable has emitted a value. You can combine the two observables like I said in my previous answer. Another solution could be to perform the second call in the subscribe of the first one. If this diagnostic is not correct perhaps could you provide an minimalist stackblitz or equivalent.

Regards,
Arnaud.

sandeep...@gmail.com

unread,
Oct 24, 2020, 3:54:55 PM10/24/20
to Angular and AngularJS discussion
Hi Arnaud,
Thanks for your reply but this appears to be very complex for  me . I am unable to chain my request.

my first call - 
getsession() in below format
{
    "error"null,
    "sessionID""XXXXXXXX"
}
I need to pick the session ID and then call othe function 
  getInformation ( sessionID from above response).

I tried to do the following thing but does not work as I get compile time error

this.service.getSession().pipe(
        switchMap(data =>{
          let session = data.sessionID;
          this.serice.getinformation(session );
        })
      )
I really need correct syntax for it. I struggled with all video from YouTube but no understanding.
Regards
Sandeep Shukla

Rahul Mishra

unread,
Oct 24, 2020, 4:03:30 PM10/24/20
to ang...@googlegroups.com
Will you please elaborate more on the error and if possible create a dummy code or scenario like this on https://stackblitz.com/ so that can help you.. in the above scenario will you please check the response you are getting in getSession service

Regards 
Rahul Mishra

Arnaud Deman

unread,
Oct 25, 2020, 11:11:42 AM10/25/20
to Angular and AngularJS discussion
Hi Sandeep,

I tried to make a sample project: https://stackblitz.com/edit/chainobservables
The MockService simulates the  calls to a backend.
The AppComponent uses this service, merges the resulting observables into
userData$ which is used in the template via the async pipe.

Regards,
Arnaud.

katya.ko...@gmail.com

unread,
Oct 26, 2020, 3:48:26 AM10/26/20
to ang...@googlegroups.com

היי חן

 

אני עשיתי EXPORT לכל DB, שמתי בתיקי בפרוייקט.

 

מצרפת את התיקיה הזו.

 

תודה

 

קטיה

mongo_data.rar
Message has been deleted

Sandeep Shukla

unread,
Oct 26, 2020, 2:58:09 PM10/26/20
to ang...@googlegroups.com
Hi Arnaud,

Thanks a lot for your help,indeed it helped me to understand the chaining of functionality.


Regards
Sandeep Shukla

Arnaud Deman

unread,
Oct 27, 2020, 5:31:19 AM10/27/20
to Angular and AngularJS discussion
Hello Sandeep,
You're welcome ;)
Responsive programming isn't that easy to learn, but it's worth it.

Regards,
Arnaud.
Reply all
Reply to author
Forward
0 new messages