Or Yaacov
Or Yaacov

Reputation: 3880

Angular 6: Cannot set property of undefined

I have a weird error, at my application the prevents me from moving forward. I have a variable of type any[], which using me as a data source of a table:

<table mat-table  [dataSource]="EcoSummaryList">

the variable is initialized into an empty array. But when I'm trying even to assign something into the array, I'm getting the following error:

core.js:12632 ERROR TypeError: Cannot set property 'EcoSummaryList' of undefined at push../src/app/eco-summary/eco-summary.component.ts.EcoSummaryComponent.OnSummaryLoad

Here's the relevant code:

public EcoSummaryList: any[] = [];

OnSummaryLoad(data: any) {
  this.EcoSummaryList = [];
  console.log(data);

  if (data == undefined || data == null) return;
  this.EcoSummaryList = data;

}

The data object is all right, and the data is coming all right and it used to work in the past.

I tried to look at other posts to find the answer without any success and I also tried to initialize the array at the constructor, at ngOnInit and to create the var as from type Array<MyClass> and then call new Array<MyClass>();

nothing seems to work, any advice?

update: even when I comment out the <table> and just assigned [] into the var I'm still getting the same error.

Upvotes: 3

Views: 11863

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39432

Looks like OnSummaryLoad is not being considered as a Component Method. Not sure why. You might want to try the arrow function syntax. Using that should fix it:

public EcoSummaryList: any[] = [];

OnSummaryLoad = (data: any) => {
  this.EcoSummaryList = [];
  console.log(data);

  if (data == undefined || data == null) return;
  this.EcoSummaryList = data;

}

Upvotes: 2

Related Questions