我的 react-native 应用程序中有以下简单的传奇:
import {call, put, take} from 'redux-saga/effects';
import firebase from 'react-native-firebase';
export function* signupWithEmailPassword(action: AuthAction) {
const {email, password} = action.payload;
try {
const user = yield call(doRegister, email, password);
yield put({type: 'SIGNUP_SUCCESS', payload: user});
} catch (error) {
yield put({type: 'SIGNUP_FAILURE', error});
}
}
function* doRegister(email: string, password: string) {
return firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password)
.catch((error) => {
const {code, message} = error;
console.log('in doRegister: error ' + code + ' - ' + message);
});
}
如果使用无效的电子邮件调用 saga,则 firebase 将抛出类似“auth/invalid-email”的错误。这很好,也是意料之中的,但由于某种原因yield call(doRegister, email, password);
并没有失败,因此yield put({type: 'SIGNUP_SUCCESS', payload: user});
即使它应该回退到catch
.
我究竟做错了什么?
编辑:
更改doRegister
为此,会导致相同的问题:
function* doRegister(email: string, password: string) {
return firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password);
}