【发布时间】:2017-02-21 19:21:16
【问题描述】:
我尝试使用 redux-saga 处理来自服务器的 Unauthorized 错误。这是我的传奇:
function* logIn(action) {
try {
const user = yield call(Api.logIn, action);
yield put({type: types.LOG_IN_SUCCEEDED, user});
} catch (error) {
yield put({type: types.LOG_IN_FAILED, error});
}
}
我这样获取数据:
fetchUser(action) {
const {username, password} = action.user;
const body = {username, password};
return fetch(LOGIN_URL, {
method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
})
.then(res => {
res.json().then(json => {
if (res.status >= 200 && res.status < 300) {
return json
} else {
throw res
}
})
})
.catch(error => {throw error});
}
但无论如何,当我期望{type: 'LOG_IN_FAILED', error: 'Unauthorized'} 时,结果是{type: 'LOG_IN_SUCCEEDED', user: undefined}。我的错误在哪里?如何正确使用 Redux-Saga 处理错误?
【问题讨论】:
标签: javascript redux fetch redux-saga