SGN SGN - 2 months ago 7
TypeScript Question

ORIGINAL EXCEPTION: Cannot read property 'Value' of undefined

I get this error

cannot read property 'Value' of undefined
, what am i doing wrong?

years: any[] = [];

ngOnInit() {
for (let i = 1970; i <= new Date().getFullYear(); i++)
{
this.years.push({'Value': i});
}
}

Answer

cannot read property 'Value' of undefined

clearly you are trying to use .Value on something that does have it. e.g.

this.years.push({'Value': i});
let year = undefined;
year.Value; // BANG

Probably you want something like:

this.years.push({'Value': i});
let year = this.years[0];
year.Value; // Okay
Comments