0

I have the following issue : I have a request "http get" and I can not receive data, since this is received after loading the entire app.

SERVICES

import { Injectable } from '@angular/core';
import { Http, Jsonp, Headers, Response, RequestOptions, Request, RequestMethod } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

@Injectable()
   export class ChartsAPI {
    token: JSON;
    data_json: any;

    constructor(private http: Http, private jsonp: Jsonp) {
       this.token = JSON.parse(localStorage.getItem('tokenUser'));
      }

    getBestSeller( filter: JSON ): Observable<any[]> {
       const auth = `Bearer ${this.token}`;
       console.log(filter);
       const headers = new Headers() ;
       headers.append('Accept', 'application/json');
       headers.append('Authorization', auth);
       const options = new RequestOptions({ 'headers': headers });
       this.http.get('https://coimco.herokuapp.com/api/products', options)
                 .map(res => res.json())
                .subscribe(
                  data => this.data_json = data,
                  err => console.log(err),
                  () => console.log(this.data_json),
                );
       return this.data_json;

      }
}

Component

  getSeller(filter: JSON) {

     console.log(this._chartAPI.getBestSeller(filter));

 }

this console browser, the API response is the last to be displayed, when should the first

Screen Shot: enter image description here

Saravana
  • 37,852
  • 18
  • 100
  • 108

0 Answers0