Gerald Hughes
Gerald Hughes

Reputation: 6159

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});
    }
}

Upvotes: 3

Views: 491

Answers (1)

basarat
basarat

Reputation: 275819

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

Upvotes: 2

Related Questions