Show JSON with API from Angular 4

0

I try to show my Object from a GET service but I can only see [Object] [object].

Service:

return this.http.get('http://localhost:3000/words').map((res: Response) => res.json());

Controller:

export class WordsComponent implements OnInit {
  words: Word[];
  private _productService;

    constructor(servicio : WordsService, public http: HttpClient) {
      this._productService = servicio;
      this.words = this._productService.getWords();

      console.log("Ver words " + JSON.stringify(this._productService.getWords()));


    }

     ngOnInit(): void {}

JSON:

[
  {
    "id": 1,
    "name": "test",
    "translate": "prueba",
    "created_at": "2018-06-10T21:15:06.000Z",
    "updated_at": "2018-06-10T21:15:06.000Z"
  },
  {
    "id": 2,
    "name": "test-one",
    "translate": "prueba-uno",
    "created_at": "2018-06-10T21:15:22.000Z",
    "updated_at": "2018-06-10T21:15:22.000Z"
  }]
    
asked by Matuss 23.07.2018 в 15:44
source

1 answer

1

You are confusing concepts:

this.words = this._productService.getWords();

The words attribute does not save the result of the AJAX call, but an observable one. You have to subscribe to the observable to get the result:

this.words : Observable<Words[]> = this._productService.getWords();
this.words.subscribe(data: Words[] => {
  console.log("Ver words " + JSON.stringify(data));
  //llamar a funciones que trabajen con los datos
}
    
answered by 23.07.2018 в 15:57