I am wanting to stop a loop that i have when it matches a condition but its just not breaking ive tried try catch loops with throw e and throw new typeerror but its just not breaking how do i get it to break here is the code i want to break
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");
}
}
I want to make it stop when the user input in state.username and state.password are equal to the one in the variable Res but i cant get it to stop looping how do i do this.
this is the function that calls this one
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)
);
});
}
edit
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");
}
});
}
error is Cannot read property 'state' of undefined
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)
);
});
}
loops through but stops on the first one weather its true or false