Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Cannot read property 'push' of undefined in [null]

Tags:

typescript

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");         }     } } 
like image 980
Ng2-Fun Avatar asked Feb 28 '16 22:02

Ng2-Fun


People also ask

How to fix Cannot read properties of undefined reading push')?

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.

Can't access property push history is undefined?

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 does Cannot read property of undefined mean?

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.


1 Answers

The array needs to be initialized:

stringArr = []; 
like image 137
Ng2-Fun Avatar answered Sep 29 '22 05:09

Ng2-Fun