Attach data['_body'] value with variable to use it in another function

  • Everything up to the post() call can be done much more simply just using HttpParams, with the added benefit that you don’t have to manually stringify anything
  • If this code is in a provider, you almost never want to be typing subscribe. If it’s in a page, you never want to be directly interacting with HttpClient. Split those two things up. If you absolutely have to modify state from inside of function1(), use the tap operator to do it. I’m going to assume for the rest of this discussion that we’re writing a provider.
  • Don’t use the console or JSON.stringify() to poke around in the internals of things. Read their public API documentation.
  • Therefore, you should never know of the existence of a _body property and should not be accessing it directly.
  • post is generic, so declare an interface (let’s call it Thingy) and let HttpClient do the work.
  • Always give every function you write a declared response type. function1()'s should be Observable<Thingy> or Observable<SomethingGeneratedFromThingy>.
  • The operator you are looking for to chain asynchronous operations here is mergeMap.
1 Like