0

I have a service that requests data from a get method, I'd like to map the response to an object storing some Ids and use those Ids to make other http requests. I was told this isn't usually done in a callback manner, I looked at this How do I return the response from an asynchronous call? but I don't think it's the usual way to implement services, any hints are very appreciated.

Tried adding in onInit/constructor method in angular to be sure the object was filled before other methods were called without success.

@Injectable ()
export class ContactService  {
   storeIds;

   getIds(callback: Function) {
   this.http.get<any>(IdsUrl, Config.options).subscribe(res => {
       callback(response);
   });

   getIds(res => {
        this.storeIds = {
          profileId: res.profile,
          refIds: res.refIds
        }
     }
   )

   // this.storeIds returns undefined as it's an async call
   this.http.post<any>(WebserviceUrl + this.storeIds.profileId , data, headers )

   // .....Many other web services that relay on this Ids
}
7
  • what is your problem? what issue you are facing in which line of your code? Commented Mar 8, 2019 at 4:49
  • Do you want chain of remote calls after getting the response from getIds call? I didnt understand that part. Or is that you want to put the response you got somewhere so that you can use it for future api calls. Can you make it clear? Commented Mar 8, 2019 at 4:52
  • I want to store this Ids somewhere for future api calls, without having to enter in a bunch of callbacks to access them. Commented Mar 8, 2019 at 4:57
  • Are u going to make all those calls from contact service or from other services? I just wanted to know if you want this response data outside the contact service? Commented Mar 8, 2019 at 5:06
  • Yes the intention is to call it from other services as ´this.myService.postSomething(data, callback)´ having this id's already existing Commented Mar 8, 2019 at 5:08

2 Answers 2

1

Just create another service called StoreIdsService. Update the response you get from your first api call 'getIds' in the StoreIdsService. The idea is to have StoreIdsService as singleton service to keep state of your storeIds. You can inject StoreIdsService in anywhere component you want to get the storeIds.

Its one of manyways to share data in angular between components.

Please refer to this answer someone has posted.

How do I share data between components in Angular 2?

Sign up to request clarification or add additional context in comments.

Comments

0

You can simply assign the service response to the storeIds property inside the subscribe method. and call the subsequent services inside it if you need.

@Injectable ()
export class ContactService  {
   storeIds;

   getIds() {
   this.http.get<any>(IdsUrl, Config.options).subscribe(res => {
       this.storeIds = {
          profileId: response.profile,
          refIds: response.refIds
        }

      this.otherapicall1();
      this.otherapicall2();
   });

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.