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

Hello,

In this code function1() I’m looking for ID by name and getting it as number from MySql database, which is appears from back-end PHP mysqli echo in console successfully.

but I’m trying to figure out, how to attach this number value from data['_body']; with fUserId variable to process it into another this.function2();. Attaching this way this.fUserId = data['_body']; does not works same as passing directly with function this.function2(fUserId);, or returning like this return this.fUserId at the end outside or inside this.http.post.

Advice would be helpful

function1() {
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let options = new RequestOptions({ headers: headers });
    let postParams = '&name=' + this.userName;  

    this.http.post("http://site.info/php/doc.php", JSON.stringify(postParams), options)
      .subscribe(data => { 

        this.fUserId = data['_body'];  

      }, error => {
        console.log(error);
      });   

      this.function2(); /// Following function to use fUserId value 
  }
  • 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

Hello @rapropos

Thank you for your answer it is very informative

Yes I have this function in a page, not in provider. I’ve removed JSON.stringify, but want use (postParams), options) to get value by inputted name, because at this moment I’m just reading about HttpParams, as I’m really new with Type Script and Ionic2.

Would be helpful, if you can give me example based on my code or even goal, I can’t find clear example of Observable function with Post, to understand how properly use it in this case.

Also I’ve putted this.function2(); inside this.http.post, now it works, but after your answer, I’m not sure if it is proper way to manipulate with data['_body'];

IMHO, that is a mistake. Put all interaction with the network in a provider.

Follow the idioms described in here for best results.

1 Like

@rapropos Thank you for answer

Hello @rapropos, can you check my post please, I’ve tried many things, but nothing helps…

if provider is only reason, would be good if you can somehow explain this, but question below does not really attached to question in this topic, seem like it is something else, because I’m just trying to input value, and function is called with the button

I can’t get proper echo. and also it is strange, why it happened suddenly withou any change, when everything was ok with same code and back-end

thank you for support

Since you seem to have listened to exactly zero of the suggestions I made in this thread, I’m not sure what making any further ones in another thread would achieve. Best of luck to you.