I have a code that fills a list with the result of a service:
dataList: number[]
getMethod(){
this.service.get().subscribe((res:any)=>{
this.dataList = res.dataList
})
}
In between the declarations of dataList and getMethod(), I declare the chart with:
public lineChartData: ChartDataSets[] = [
{ data: this.dataList, label: 'Label1' },
{ data: ..., label: 'Label2' },
];
However the following error will pop up:
Property 'dataList' is used before its initialization.ts(2729)
If I instantiate dataList to [], the error will no longer be there, but dataList will be empty. How could I fill such dataSet with the data returned from the service?
CodePudding user response:
You have to init the dataList, because now you are trying to assign undefined to DataSet. After this you have to update chart at the end of subscription. You didn't attach full code, but I assume it would look like that:
getMethod(){
this.service.get().subscribe((res:any)=>{
this.dataList = res.dataList
this.chart.update()
})
}