Warm tip: This article is reproduced from serverfault.com, please click

using variable after the subscribe block angular ionic

发布于 2020-11-27 21:08:25

I m trying to use variable after the subscribe block,but I can't look at snippet:

async check(){
   
  this.VersionNumber = await this.appVersion.getVersionNumber()
  
    this.settingsService.getjson().subscribe(item=>{
     this.djson = item
    alert(this.djson)  /// here work
 
  });
   alert(this.djson)  /// here not work, I got : [object:Object]
   
   
    if((this.VersionNumber !== this.djson)  ){
    this.presentAlertConfirm();
 
  }
    
  
  }

settings.service.ts

getjson(){
     return this.http.get('./assets/jsonfile.json').pipe(map(res=> res['currentVers']))
     }
Questioner
christo
Viewed
11
Jonathan Lopez 2020-11-28 13:54:20

Subscriptions are asynchronous that is why you get that error, you could create a function to have to logic to validate the data and call this function inside of the subscribe to have the value set when you want to use it.