我想停止一个循环,当它匹配条件但它只是没有破解试图尝试使用throw e捕获循环并抛出新的typeerror,但它只是不打破如何获得它打破这里是代码我想打破React js break when match happens
LoginAuth = (Res) => {
try {
if (Res.username === this.state.Username && Res.password === this.state.Password) {
return (
this.setState({
isLoggedIn: true,
}, function() {
}),
this.hideModal()
);
} else {
console.log("Gets to login auth and false");
}
}
我想让它停下时state.username和state.password用户输入都等于一个可变RES,但我不能得到它停止循环如何做到这一点。
这是调用本
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.forEach(function (Res) {
return (
console.log("ID=" + Res.id),
console.log("Username=" + Res.username),
console.log("Password=" + Res.password),
self.LoginAuth(Res)
);
});
}
编辑功能
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.some(function (Res) {
console.log("ID=" + Res.id);
console.log("Username=" + Res.username);
console.log("Password=" + Res.password);
if (Res.username === this.state.Username && Res.password === this.state.Password) {// the error is on this line saying it doesnt know what Username or Password are in the state
return (
this.setState({
isLoggedIn: true,
}, function() {
}),
this.hideModal()
);
} else {
console.log("Gets to login auth and false");
}
});
}
错误是无法读取通过未定义
LoginAuth = (Res) => {
if (Res.username === this.state.Username && Res.password === this.state.Password) {
return (
this.setState({
isLoggedIn: true,
}, function() {
}),
this.hideModal()
);
} 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.every(function (Res) {
console.log("ID=" + Res.id);
console.log("Username=" + Res.username);
console.log("Password=" + Res.password);
return (
self.LoginAuth(Res)
);
});
}
循环的特性 '状态',但停在第一个天气其真假
您使用的承诺有没有falsy价值?如果是,那么你需要在Promise.reject中返回异常。 – CuriousSuperhero