2

I had a problem with posting data in angular 2.

postOrder() {

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    let data=JSON.stringify({
                          customer_id: this.cusid,
                          mode_code: this.data2code,
                          order_totalpayment: this.dataprice,
                          order_status: this.status,
                          order_remarks: this.remarks,
                          order_type: this.ordertype
                        });


  this.http.post('http://JSON-API',data,headers)
  .map(res => res.json())
  .subscribe(res => {
  console.log("Success Order No.: "+res);
  this.ordernum = res;
  });

        let data2=JSON.stringify({
                        order_no: this.ordernum,
                        prod_id: this.dataid,
                            order_product_quantity: this.quantity,
                            order_subtotal: this.dataprice
                            });

  this.http.post('http://JSON-API',data2,headers)
  .map(response => response.json())
  .subscribe(response => {
  console.log("Order List No.: "+response);
  console.log(this.ordernum);
  });

}

My problem is I cannot post the res data but when I use console log to it, it shows me the right data. What I did is I pass the res data to ordernum variable.

this.http.post('http://JSON-API',data,headers)
      .map(res => res.json())
      .subscribe(res => {
      console.log("Success Order No.: "+res);
      this.ordernum = res;
      });

And then I am trying to POST it to the order_no to my JSON API.

let data2=JSON.stringify({
                        order_no: this.ordernum,
                        prod_id: this.dataid,
                            order_product_quantity: this.quantity,
                            order_subtotal: this.dataprice
                            });

The correct data shows in the console but in my JSON API, the order_no is always zero. All the data I POST works except for the order_no.. What should I do for me to solve this. Hope you can help me. Thank you in advance.

3
  • do you send the second http post in the subscribe function in the first http post? Commented Sep 10, 2017 at 6:24
  • No, that is only what exactly I did. Commented Sep 10, 2017 at 6:26
  • so this.ordernum is undefined, see my answer Commented Sep 10, 2017 at 6:31

1 Answer 1

1

this.ordernum is not defined unless the first http post is resolved, put the second http post inside the subscribe function to have it:

 this.http.post('http://JSON-API',data,headers)
  .map(res => res.json())
  .subscribe(res => {
      console.log("Success Order No.: "+res);
      this.ordernum = res;
      let data2=JSON.stringify({
                        order_no: this.ordernum,
                        prod_id: this.dataid,
                            order_product_quantity: this.quantity,
                            order_subtotal: this.dataprice
                            });
      this.http.post('http://JSON-API',data2,headers)
      .map(response => response.json())
      .subscribe(response => {
          console.log("Order List No.: "+response);
          console.log(this.ordernum);
      });
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! It worked for me. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.