I'm trying to manipulate a variable after an HTTP call.
I want to use functions such as .push()
or .forEach()
, but it doesn't allow me because data isn't assigned to the variable at first.
I get an error in the console:
Cannot read properties of undefined (reading 'forEach')
My Component:
import {Component, OnInit} from '@angular/core';
import {TablesService} from "../tables.service";
@Component({...})
export class BigQueryComponent implements OnInit {
fetchedData: any;
DATA: any;
constructor(private tableService: TablesService) {
this.tableService.fetchAPI().subscribe((data) => {
this.fetchedData = data;
})
}
ngOnInit(): void {
this.getQueryData();
}
getQueryData() {
this.fetchedData.forEach( dataset => {
this.DATA.push( dataset );
});
}
}
My Service:
export class TablesService {
constructor(private http: HttpClient) {}
fetchAPI() {
return this.http.get('https://.../products');
}
}
How could I do this? I've tried to add Observable to the HTTP function, but I'm not sure how should it be done correctly.