Cannot read property X of undefined: declaring optional values in ngModel for lazy people
Cannot read property ‘x’ of undefined.
Scenario: Your are trying to bind some value to an input and that input contains some pipe that would say transform the value it gets to uppercase.
Issue: Angular is going to be on your ass now and throw about 1.56 billions errors because it’s trying to uppercase a string that does not exist.
Solution 1: you could either check if there is an existing value in the pipe itself, but that would not work for instance in our specific case since uppercase is an angular pipe and you would need to extend the pipe in your class to change it’s behavior which is not very recommended.
Solution 2?: Simply add a question tag to he property. So let’s say your ngModel prop is user.age and age is undefined for now, simply write :
user?.age
Voila