我正在努力解决一些严重的问题。我试图在我的代码中使用几个数组方法,如push,includes,filter。它们都不起作用。
ERROR TypeError: Cannot read property 'push' of undefined
简单使用推送:
users: User[];
user = new User('','','','','','','','','');
addUser(user){
this.usersService.addUser(user)
.subscribe(
user => this.users.push(user),
error => this.errMsg = <any> error);
}
更新:意外地注释掉了用户的初始化。
this.getUsers();
if(this.users.find(this.checkLogin) === undefined){
console.log('OK no login like this');
}else{
console.log('Login like this exists');
}
checkLogin(element){
return element === this.user.login;
}
getUsers(){
this.usersService.getUsers()
.subscribe(
users => this.users = users,
error => this.errMsg = <any> error);
}
仍然收到错误:
错误TypeError:无法读取未定义
答案 0 :(得分:4)
变化:
users: User[];
要:
users: User[] = [];