Kartiikeya
Kartiikeya

Reputation: 2516

TypeError: Cannot read property 'push' of undefined-IONIC2

I'm trying to push iterated value to array

json_resp is Json response.

my Typescript code

export class hello {

  CategoryLst:any[];

  var catIndex,BillerIndex;

  for(catIndex = 0; catIndex <= json_resp.category.length; catIndex++) {
    var Clst = json_resp.category[0].categoryName;
    this.CategoryLst.push(Clst);
  }
}

while trying to execute its throwing error as

ORIGINAL EXCEPTION: TypeError: Cannot read property 'push' of undefined

is there something am i missing ??

Upvotes: 0

Views: 3822

Answers (2)

sebaferreras
sebaferreras

Reputation: 44659

CategoryLst:any[] is just specifying the type of the array, but not assigning it, so by default the value of that array would be undefined.

In order to initialize it in the same declaration, you should do it this way:

CategoryLst:any[] = [];

Upvotes: 3

Yashwanth Rao
Yashwanth Rao

Reputation: 246

try this

CategoryLst:any[] = [];

Upvotes: 4

Related Questions