【发布时间】:2020-10-11 01:33:57
【问题描述】:
我试图从我的登录方法中捕获一个错误,然后在我的代码中显示一个警报。我收到一条警告说“未处理的承诺拒绝...”
export default function Login({navigation}){
const [email, setEmail] = React.useState('');
const [password, setPassword] = React.useState('');
const [showAlert, setShowAlert] = React.useState(false);
const [showAlert2, setShowAlert2] = React.useState(false);
const { signIn } = React.useContext(AuthContext);
const submit = async() => {
if (email === '' || password === '') {
setShowAlert(true);
} else {
signIn(email, password).catch(error =>
setShowAlert2(true)); // THIS NEVER GETS TRIGGERED, WHY?
}
}
...
}
signIn 在我的 App.js 中定义如下:
const authContext = React.useMemo(() => {
return {
signIn: (email, password) => {
auth().signInWithEmailAndPassword(email.email, password.password)
.then((res) => {
setIsLoading(false);
setUser(res.user.uid);
})
.catch(error => {
throw error; // This is the error that should be catched in "submit"
})
},
signUp: () => {
setIsLoading(false);
setUser("test");
},
signOut: () => {
setIsLoading(false);
auth().signOut().then(() => console.log('User signed out!'));
setUser(null);
}
};
}, []);
如您所见,我在某些时候执行“抛出错误”。这就是我想在上面的提交 const 中捕获的错误。
这是我得到的错误:
TypeError: undefined is not an object (evaluating 'signIn(email, password).catch')
【问题讨论】:
标签: react-native