代码之家  ›  专栏  ›  技术社区  ›  Sumanth Jois

Redux Saga的Firebase身份验证不起作用

  •  9
  • Sumanth Jois  · 技术社区  · 7 年前

    我正在尝试使用firebase auth创建新用户。我正在使用redux saga进行上述调用。

    下面是我的 传奇故事js公司:

    import { takeLatest, put } from 'redux-saga/effects';
    import { SIGN_UP, AUTHENTICATION_FAILED, SIGN_UP_SUCCESS} from '../actions/index';
    import firebase from 'firebase';
    
    
    function* signUp(action){
        console.log("about to call authentication"); // This being printed
        firebase.auth().createUserWithEmailAndPassword(action.user.email, action.user.password)
                .then(function*(result){
                        console.log("success"); // Not being loged
                        yield put({SIGN_UP_SUCCESS, user: action.user});
                }).catch(function*(error){
                        console.log("failed"); //Not being loged
                        let error_message = { code: error.code, message: error.message};
                        yield put({AUTHENTICATION_FAILED, error: error_message});
                });
    }
    
    function* rootSaga(){
      yield takeLatest(SIGN_UP, signUp);
    }
    
    export  default rootSaga;
    

    里面的代码 then catch 未执行,正在调用注册生成器函数。谁能告诉我我做错了什么吗?

    2 回复  |  直到 7 年前
        1
  •  24
  •   Alex    7 年前

    A更多 redux-saga 编写代码的惯用方式如下所示:

    function* signUp(action){
      try {
        const auth = firebase.auth()
        const result = yield call(
          [auth, auth.createUserWithEmailAndPassword],
          action.user.email,
          action.user.password
        )
    
        yield put({ type: SIGN_UP_SUCCESS, user: action.user });
      } catch (error) {
        const error_message = { code: error.code, message: error.message };
    
        yield put({ type: AUTHENTICATION_FAILED, error: error_message });
      }
    }
    

    请参见 https://redux-saga.js.org/docs/api/#callcontext-fn-args 对于 call 的文档。

        2
  •  0
  •   Krasimir    7 年前

    上面代码中的问题是,您将生成器作为 then catch . 它们应该只是函数,因为没有任何代码可以迭代它们。所发生的是创建了一个生成器迭代器。

    您当然可以自由使用承诺,但我建议您遵循redux传奇的方式,并通过您的 createUserWithEmailAndPassword yield call .