[英]Firebase authentication with Redux-Saga not working
我正在嘗試使用firebase auth創建一個新用戶。 我正在使用redux-saga進行上述調用。
下面是我的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
不被執行,注冊生成函數被調用。 誰能告訴我我做錯了什么?
編寫代碼的更多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 });
}
}
有關call
的文檔,請參閱https://redux-saga.js.org/docs/api/#callcontext-fn-args 。
上面代碼中的問題是你將生成器作為then
和catch
處理程序傳遞。 它們應該只是函數,因為沒有任何代碼可以迭代它們。 會發生什么是你創建了一個生成器迭代器。
您當然可以自由使用promises,但我建議您遵循redux-saga方式並傳遞createUserWithEmailAndPassword
以yield call
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.