我想在执行数组映射后调用函数来检查任何与我的变量匹配的值,但我想在单独的函数中执行它,所以我尝试调用函数并得到错误TypeError :无法读取未定义的属性“LoginAuth”
我的代码就是这个
LoginAuth = (Res) => {
if (this.username === this.state.Username) {
console.log("get to login auth and true");
} else {
console.log("Gets to login auth and false");
}
}
CheckLoginAuth() {
console.log("gets to check login auth");
console.log(this.state.AuthRes);
console.log("Username=" + this.state.Username);
console.log("Password=" + this.state.Password);
this.props.AuthRes.map(function (Res) {
console.log("ID=" + Res.id);
console.log("Username=" + Res.username);
console.log("Password=" + Res.password);
this.LoginAuth(Res);//react is actually throwing the error here.
})
}
当我单击表单上的一个按钮来调用它时,它会被调用它是怎么回事。
答案 0 :(得分:1)
this
函数中的 map
不会转到您想要的this
,实际上未定义本身。
function
电话,但是如果你想保持这样的话,就这样吧。
试试这个:
通知:
var self = this;
并致电self.login...
LoginAuth = (Res) => {
if (this.username === this.state.Username) {
console.log("get to login auth and true");
} else {
console.log("Gets to login auth and false");
}
}
CheckLoginAuth() {
console.log("gets to check login auth");
console.log(this.state.AuthRes);
console.log("Username=" + this.state.Username);
console.log("Password=" + this.state.Password);
var self = this;
this.props.AuthRes.map(function (Res) {
console.log("ID=" + Res.id);
console.log("Username=" + Res.username);
console.log("Password=" + Res.password);
self.LoginAuth(Res);//react is actually throwing the error here.
})
}