我的react-native app中有以下简单的传奇:
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);
}
答案 0 :(得分:0)
改变你的传奇
function* doRegister(email: string, password: string) {
const response= yield firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password);
return response; /*this will ensure it does not return before you get the response*/
}
答案 1 :(得分:0)
这就是我的工作方式
export function* loginREST( email, password ) {
// change this to whatever firebase call you need
return yield call(
[ firebase.auth(), firebase.auth().signInWithEmailAndPassword ],
email,
password,
);
}
export function* login( action ) {
try {
const response = yield call(
loginREST,
action.payload.email,
action.payload.email,
);
const { email, uid } = response.user;
// for some weird reason passing back the entire response object gave
// me an infinite loop so I fixed that by only passing the stuff I need
yield put( loginSuccessAction( { email, uid } ) );
}
catch ( error ) {
yield put( loginFailedAction( error ) );
}
}
答案 2 :(得分:0)
在你的世外桃源中:
export function* signupWithEmailPassword({email , password}) {
try {
const user = yield call(doRegister, email, password);
yield put({type: 'SIGNUP_SUCCESS', payload: user});
} catch (error) {
yield put({type: 'SIGNUP_FAILURE', error});
}
}
如果有任何错误,例如电子邮件格式不正确,请使用
export function* signupWithEmailPassword({email , password}) {
try {
const user = yield call(doRegister, email.trim(), password);
yield put({type: 'SIGNUP_SUCCESS', payload: user});
} catch (error) {
yield put({type: 'SIGNUP_FAILURE', error});
}
}