Observable Undefined

后端 未结 1 1488
甜味超标
甜味超标 2020-12-20 05:58

I\'m trying to extract the values from an observable, my subscription (component) code is as followed:

this.checkoutService.getDisabledDate().subscribe
(date         


        
相关标签:
1条回答
  • 2020-12-20 06:23

    It's unclear from your question where "outside" is, but if it's after the call where you get the Observable then this is expected behavior

    someMethod() {
      this.checkoutService.getDisabledDate()
      .subscribe(
        // anything here is executed sometimes later when the response from the server arrives
        dates=>{ 
          this.formattedDate=dates;
          // code that depends on the result goes here
        }, 
        (error:any)=>{console.log(error)}
      );
      // this is executed first
    }
    

    You can't get the value outside of subscribe. You can use map and return the result for the caller to subscribe, like done in getDisabledDate or you move the code to one of the callbacks.

    0 讨论(0)
提交回复
热议问题