Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
comprobando el objeto json de retorno para datos mecanografiados y angular2

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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 .

over 4 years ago · Santiago Trujillo Report

0

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); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!