0

I'm working with angular2. Now, I'm in trouble to get a value inside subscribe(). I know it will return null. How can I return the correct value?

Here's my code. Anyone give me advice, it would be very helpful. Thank you :)

this.deliveryService.deliveryCancel(rowInfo.branchId, rowInfo.id).subscribe(
              res => {
                var data = res.json();
                this.getDeliveryList();
                this.msgs.push({ severity: 'success', summary: 'Info', detail: rowInfo.deliveryStatus });
              },
              err => {
                this.errMessage = <any>err;
                this.msgs.push({ severity: 'error', summary: 'Error', detail: this.errMessage });
              }
            );
            
            console.log(data);

deliveryCancel()

 deliveryCancel(branchId, deliveryId) {
    const headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.put(this.testUrl + '/admin/branches/' + branchId + '/deliveries/' + deliveryId + '/cancel', {
      headers: headers
    })
      .catch(handleError);
  }
Mingyu Jeon
  • 1,755
  • 5
  • 23
  • 40

2 Answers2

0

You cannot access res.json() like that. And initialise 'data' as class variable. Try this.

data = {};

this.deliveryService.deliveryCancel(rowInfo.branchId, rowInfo.id).subscribe(
          res => {
            this.data = res['_body'];
            this.getDeliveryList();
            this.msgs.push({ severity: 'success', summary: 'Info', detail: rowInfo.deliveryStatus });
          },
          err => {
            this.errMessage = <any>err;
            this.msgs.push({ severity: 'error', summary: 'Error', detail: this.errMessage });
          }
        );

        console.log(data);
raj m
  • 1,863
  • 6
  • 21
  • 37
0

This is not an angular question but more a javascript question about asynchronous behaviour breaking flow of execution.

Suscribing means that on any error or success returned by deliveryCancel(), the res function or err function will be called.

Your console.log(data) is only executed once, and never part of those functions.

This is what you are after:

    this.deliveryService.deliveryCancel(rowInfo.branchId, rowInfo.id).subscribe(
      res => {
        this.data = res['_body'];
        this.getDeliveryList();
        this.msgs.push({ severity: 'success', summary: 'Info', detail: rowInfo.deliveryStatus });
console.log(data);
      },
      err => {
        this.errMessage = <any>err;
        this.msgs.push({ severity: 'error', summary: 'Error', detail: this.errMessage });
 console.log(data);
      }
    );
Arinaud
  • 155
  • 6
  • You will have to call deliveryCancel as well. – Arinaud Feb 23 '17 at 07:10
  • I personnaly subscribe to observables in ngAfterViewInit – Arinaud Feb 23 '17 at 07:10
  • Thanks for the reply! However, it's not what I want to know. I wanted to know how to use 'data' outside subscribe() – Mingyu Jeon Feb 23 '17 at 08:44
  • You said 'It returns JSON data, but if I print it, it prints 'null' ' in a comment, giving the impression that the call is successful. Console.log will let you know the outcome of your call. Please confirm it is an Object and not 'undefined'. Once this.data has been assigned you can simply access it in your template following [this method](https://angular.io/docs/ts/latest/guide/displaying-data.html), or in the component in any function/getter using this.data. I repeat, that will ONLY be available after subscribe instruction execution and after deliveryCancel has been called. – Arinaud Feb 23 '17 at 09:34