1

我正在嘗試使用sagas中間件來設置通過firebase的redux註冊和驗證流程。不過我越來越:redux-sagas發電機內部回調失敗

Module build failed: SyntaxError: Unexpected token (16:3) 

    14 | registerUser(action.creds).then(response => { 
    15 |  if(response.payload.errorCode) 
> 16 |   yield put({type: 'REGISTRATION_FAILED', message: {msg_type: 'error', msg_body: response.payload.errorMessage}}) 
    |  ^
    17 |  else 
    18 |   yield put({type: 'REGISTRATION_SUCCESSFUL', user: response}) 
    19 | }); 

有點普通,但移動該行代碼周圍似乎產量特別不喜歡在回調之中。但是,我沒有看到任何地方這是特別禁止的。有沒有更好的方法來做到這一點?這是傳奇發電機。

export function* registerUserSaga(action){ 
    console.log('registerUser called') 
    console.log(action) 

    registerUser(action.creds).then(response => { 
     if(response.payload.errorCode) 
      yield put({type: 'REGISTRATION_FAILED', message: {msg_type: 'error', msg_body: response.payload.errorMessage}}) 
     else 
      yield put({type: 'REGISTRATION_SUCCESSFUL', user: response}) 
    }); 
} 

這裏是火力點登錄功能:

export default FireBaseTools = { 
    registerUser: (creds) => { 
     return firebaseAuth.createUserWithEmailAndPassword(creds.email, creds.password).then(user => { 
      return user; 
     }).catch(error => { 
      return { 
      errorCode: error.code, 
      errorMessage: error.message 
      } 
     }); 
    } 
} 

編輯:最初我認爲這是由if/else語句的原因,但回調似乎是真正的罪魁禍首

回答

0

你應該使用註冊用戶函數Promise;

類似的東西:

registerUser: creds => { 
    return firebaseAuth.createUserWithEmailAndPassword(creds.email, creds.password) 
    .then(user => ({user})) 
    .catch(error => ({error})) 
} 

export function* registerUserSaga(action){ 

    const {error, user} = yield call(registerUser,action.creds); 
    error 
    ? yield put({type: 'REGISTRATION_FAILED', 
       message: {msg_type: 'error', msg_body: error.message}}) 

    : yield put({type: 'REGISTRATION_SUCCESSFUL', user}); 
}