Javascript 当firebase的电子邮件或密码错误时,应用程序崩溃
因此,我使用的是最新版本的react native,firebase+react native form validator。 当我尝试使用正确的电子邮件和密码与现有用户登录时,一切都很好,但是,当我插入错误的电子邮件或密码时,应用程序崩溃,我出现以下错误: 相关代码行为:Javascript 当firebase的电子邮件或密码错误时,应用程序崩溃,javascript,firebase,react-native,firebase-authentication,Javascript,Firebase,React Native,Firebase Authentication,因此,我使用的是最新版本的react native,firebase+react native form validator。 当我尝试使用正确的电子邮件和密码与现有用户登录时,一切都很好,但是,当我插入错误的电子邮件或密码时,应用程序崩溃,我出现以下错误: 相关代码行为: state={email:'',密码:'',错误:'',加载:false} 用户登录=()=>{ this.setState({loading:true}); 这个。验证({ 电子邮件:{required:true,em
state={email:'',密码:'',错误:'',加载:false}
用户登录=()=>{
this.setState({loading:true});
这个。验证({
电子邮件:{required:true,email:true},
密码:{required:true,minlength:5}
});
if(this.isFormValid()){
firebase.auth().signiWithEmailandPassword(this.state.email,this.state.password)
.然后(()=>Actions.partyzmain())
.catch(设置超时(()=>{
this.setState({error:'电子邮件或密码为inccorect!',密码:'',加载:false});
}, 5000));
}
否则{
if(this.isFieldInError('password')){
this.setState({error:'error password!',password:'',loading:false});
}
如果(此.isFieldInError(“电子邮件”)){
this.setState({error:'Invalid email address!',loading:false});
}
}
};代码>您的代码似乎正确
唯一可能的问题可能是setTimeout
,它在不同的范围内运行this.setState
,正如我在错误屏幕中看到的,该屏幕显示了计时器的回溯
我建议您使用async/await
并使用try…catch
构造处理所有丢弃的文件
对于计时器,您可以使用以下方法:
const wait = (timer) =>
new Promise((resolve) => {
setTimeout(resolve, timer);
})
总体代码为:
state = { email: '', password: '', error: '', loading: false }
userLogin = async () => {
try {
this.setState({ loading: true });
this.validate({
email: { required: true, email: true },
password: { required: true, minlength: 5 }
});
if (!this.isFormValid()) {
if (this.isFieldInError('password')) {
this.setState({
error: 'Wrong password!',
password: '', loading: false
});
}
if (this.isFieldInError('email')) {
this.setState({
error: 'Invalid email address!',
loading: false
});
}
return;
}
await firebase
.auth()
.signInWithEmailAndPassword(
this.state.email,
this.state.password
);
Actions.partyzmain();
}
catch (error) {
console.debug(error);
if (error.code.startsWith('auth')) {
await wait(1000);
this.setState({
error: 'Email or password are inccorect!',
password: '',
loading: false
});
}
}
};
您的代码似乎是正确的
唯一可能的问题可能是setTimeout
,它在不同的范围内运行this.setState
,正如我在错误屏幕中看到的,该屏幕显示了计时器的回溯
我建议您使用async/await
并使用try…catch
构造处理所有丢弃的文件
对于计时器,您可以使用以下方法:
const wait = (timer) =>
new Promise((resolve) => {
setTimeout(resolve, timer);
})
总体代码为:
state = { email: '', password: '', error: '', loading: false }
userLogin = async () => {
try {
this.setState({ loading: true });
this.validate({
email: { required: true, email: true },
password: { required: true, minlength: 5 }
});
if (!this.isFormValid()) {
if (this.isFieldInError('password')) {
this.setState({
error: 'Wrong password!',
password: '', loading: false
});
}
if (this.isFieldInError('email')) {
this.setState({
error: 'Invalid email address!',
loading: false
});
}
return;
}
await firebase
.auth()
.signInWithEmailAndPassword(
this.state.email,
this.state.password
);
Actions.partyzmain();
}
catch (error) {
console.debug(error);
if (error.code.startsWith('auth')) {
await wait(1000);
this.setState({
error: 'Email or password are inccorect!',
password: '',
loading: false
});
}
}
};
auth.js第17行等。尝试查找调用的位置\auth.js第17行等。尝试查找调用的位置\t它表示“没有与此标识符对应的用户记录。用户可能已被删除。”@AdarWallerstein changeconsole。在我对console的回答中出现错误
。debug
否则将显示红色屏幕:)它说“没有与此标识符对应的用户记录。用户可能已被删除。”@AdarWallerstein更改控制台。错误
在我对控制台的回答中。调试
否则将显示红色屏幕:)