Error: Cannot read property 'push' of undefined in [null].
class A implements OnInit { stringArr: string[]; ngOnInit() { for(let i=0; i<4; i++) { this.stringArr.push("aaa"); } } }
To solve the "Cannot read property push of undefined" error:Use the Array. isArray() method to check if the value is an array. If the value is an array, call the push() method. The push() method should only be called on valid arrays.
To fix the "Cannot read property 'push' of undefined" error with React Router, we should call the withRouter component with our route component so the history object is available in the props. import { withRouter } from "react-router"; class App extends Component { push = () => { this. props. history.
What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.
The array needs to be initialized:
stringArr = [];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With