Treat data collected through a JSON api

-1

I am collecting data provided by an API in JSON.

With the code below I can see that the data was collected inside Subscribe.

But outside of it, inside the .TS file itself I can not manipulate this data.

I tried to use the promise, but I do not think I used it correctly, because I also did not have a positive result.

How could I handle this data before displaying it on the screen?

getHour() {
this.hourService.getData()
  .subscribe(
    posts => this.posts = posts,
    error => this.errorMessage = <any>error,
  () => console.log(this.posts));     

  console.log(this.posts);
}

    
asked by anonymous 17.04.2018 / 15:32

2 answers

0

You can store this return within a global variable.

var posts = this.posts;
    
17.04.2018 / 19:31
0

So you think, man, when you make the hourService.getData an HTTP request and triggered when it completes the code inside the subscribe and executed (if it's okay with the request)

 getHour() {
    this.hourService.getData()
      .subscribe( //http disparado
        posts => this.posts = posts, //quando a requisição terminou e deu certo
        error => this.errorMessage = <any>error,//quando a requisição terminou e deu errado
      );     

      console.log(this.posts);//aqui o http ainda não terminou por isso o undefined
}
    
20.04.2018 / 16:56