0

我的 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);
}
4

3 回答 3

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*/
}
于 2018-03-11T13:15:42.960 回答
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 ) );
  }
}
于 2019-04-25T12:30:33.017 回答
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});
      }
    }

如果有任何错误,例如电子邮件格式错误,请使用 trim like

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});
          }
        }
于 2019-09-04T07:18:19.973 回答