My goal is to retrieve data from an external API using Typescript. Below is the function I have written:
export class BarChartcomponent implements OnInit{
public chart: any;
data: any = [];
constructor(private service:PostService) { }
ngOnInit(): void {
this.service.getPosts().subscribe(response => {
this.data = response;
});
this.createChart();
};
}
I am encountering an issue where the "data" variable appears as "undefined" when trying to use it for further data manipulation outside the subscribe function. I understand that this is a scope error, but I am unsure of how to resolve it.