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

347
Views
Datos API indefinidos cuando se usan en el componente

tengo este servicio para obtener datos de la API y luego guardarlos en matrices:

 export class DataService { constructor(private http: HttpClient) { } readonly baseURL = "https://localhost:44302/api"; books: Book[]; users: User[]; cards: Card[]; postId: number; httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) } getBooks() { return this.http.get(this.baseURL + '/books'); } getCards() { return this.http.get(this.baseURL + '/cards'); } getUsers() { return this.http.get(this.baseURL + '/users'); } getData() { const observableList: Observable<any>[] = []; observableList.push(this.getBooks()); observableList.push(this.getUsers()); observableList.push(this.getCards()); forkJoin(observableList).subscribe(resultList => { this.books = resultList[0] as Book[]; this.users = resultList[1] as User[]; this.cards = resultList[2] as Card[]; }); }

en mi componente, ejecuto el método getData e intento consolar. registrar los datos, pero sigue diciendo que los datos no están definidos, aunque los datos de la API aparecen en la pestaña de red de depuración:

 export class MainComponent implements OnInit { books: Book[]; users: User[]; cards: Card[]; selectedBook: Book; display: boolean = false; selectedUser: User; constructor(public dataService: DataService) { } ngOnInit() { this.dataService.getData(); //Save data in local component arrays this.books = this.dataService.books; this.users = this.dataService.users; this.cards = this.dataService.cards; console.log(this.books); // ==> "undefined" }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Las matrices asignadas dentro del componente (después de la llamada de servicio) ya se ejecutarán antes de que el servicio con getData() complete la suscripción a los tres observables. Es por eso que están asignando a datos indefinidos.

En su servicio, modifíquelo para devolver solo lo observable:

 getData() { const observableList: Observable<any>[] = []; observableList.push(this.getBooks()); observableList.push(this.getUsers()); observableList.push(this.getCards()); return forkJoin(observableList); }

En su componente, suscríbase a los datos unidos llamando a getDate() dentro de un suscriptor:

 this.dataService.getData() .subscribe(({resultOne, resultTwo, resultThree}) => { //Save data in local component arrays this.books = resultOne; this.users = resultTwo; this.cards = resultThree; }); }
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!