así que hice una llamada para verificar y ver si devuelvo un objeto con datos reales. No estoy seguro de si estoy haciendo esto mal, pero esto es lo que estoy intentando.
Aquí está mi llamada recibida que hice a mis servicios - ARCHIVO DE COMPONENTES:
resultData: any = []; barChartLabels: any =[]; getData(){ this.dataService.getData().subscribe((data) => { this.resultData = data; for(let item of this.resultData) { this.barChartLabels.push(item.name); } } } Actualmente estoy viendo this.resultData en mi consola, pero sé que después de insertar el nombre del elemento en otra matriz que se usa para enlazar en mi html, no se muestra.
Aquí está mi llamada de servicio - ARCHIVO DE SERVICIO:
getData(){ return this.http.get('my url') .map((res:Response) => res.json()); }Esto es también lo que tengo en mi archivo json:
[ { "id": 1, "name": "Test 1", "score": 34 }, { "id": 2, "name": "Test 2", "score": 92 } ]Este es mi html:
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType"> </canvas>Esto puede haber pasado a un tema diferente ahora.
Si realmente desea que esté en el componente, entonces el código debe estar en el cuerpo de la función de flecha. Algo como esto:
getData(){ this.dataService.getData().subscribe(data => { this.resultData = data; for(let resultItem of this.resultData){ console.log(resultItem); } }) } Esto se debe a que la suscripción es asíncrona. Cuando el método getData se ejecuta por primera vez, this.resultData no tiene ningún valor. No es hasta que se devuelve la respuesta Http que se establece this.resultData .
Puede utilizar el operador do para ver los datos que se devuelven. Podrías intentar algo como esto:
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; ... getProducts(): Observable<IProduct[]> { return this._http.get(this._productUrl) .map((response: Response) => <IProduct[]> response.json()) .do(data => console.log('All: ' + JSON.stringify(data))) .catch(this.handleError); }