为什么redux-saga不会在通话中抛出异常

问题描述 投票:0回答:2

我的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);
}
firebase react-native redux-saga react-native-firebase
2个回答
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*/
}

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 ) );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.