2017-07-14 40 views
0

我真的很奇怪。由於某種原因,我的put在我的catch塊中沒有在下面執行。這裏是我的傳奇:Redux-saga產量put()in catch block not executed

function* postLoginFormSaga(action) { 
    let response; 
    try { 
     // Data from emitting login action 
     response = yield call(AuthenticationApi.login, action.formData); 
     yield put(createLoginSucceedAction()); 
     yield put(push('/dashboard/summary')); 
    } catch (e) { 
     console.log(e); 
     yield put(createLoginFailAction(response)) 
    } 
} 

我的API調用,它有一個自定義的中間件來處理非2XX迴應:

static login = (formData) => { 
     return fetch('/api/login', { 
      method: 'POST', 
      body: formData, 
      credentials: 'same-origin' 
     }).then(handleFetchErrorMiddleWare) 
      .then(r => r.json()) 
    }; 

凡中間件功能是檢查responseok屬性的簡單功能object:

export function handleFetchErrorMiddleWare(response) { 
    if (!response.ok){ 
     throw Error(response.status) 
    } 
    return response 
} 

中間件工作正常,因爲我可以看到console.log(e) wh的輸出恩拋出一個異常,但yield put(createLoginFailAction(response))沒有被髮出... 見下文:

enter image description here

任何想法?謝謝

+0

是否正確'yield put(createLoginFailAction(response))'? 'response'在這裏是'undefined'。 –

+0

在'createLoginFailAction'函數中可能有錯誤,那是什麼樣的? – erikvold

回答

1

對我來說,好像createLoginFailAction函數有錯誤,如果這樣會導致你看到的結果。

+0

事實上,讀取對象時發生錯誤。謝謝... – coolboyjules