【发布时间】:2021-08-02 11:52:16
【问题描述】:
我正在使用以下 createAsyncThunk 删除数据库中的应用程序。
由于某种原因,提取无法正常工作。 handleFetchErrors 如果响应不正确则抛出错误
export const handleFetchErrors = (response: Response) => {
if (!response.ok) {
throw Error(response.statusText)
}
}
export const deleteApplication = createAsyncThunk("applications/deleteApplication", async (data: any) => {
try {
const response = await fetch(`/api/application/${data.id}`, {
method: "DELETE",
headers: {
'Accept': 'application/json',
"Content-Type": "application/json",
},
});
//fixme: call still gets fulfilled although I am throwing an error
handleFetchErrors(response);
return (data.id);
} catch (e) {
console.error(e);
}
})
在我的createSlice 中,我将这些额外的Reducers 用于大写
.addCase(deleteApplication.fulfilled, (state, action) => {
state.status = DataFetchingStatus.succeeded
state.applications = state.applications.filter(application => application.id !== action.payload)
})
.addCase(deleteApplication.rejected, (state, action) => {
state.status = DataFetchingStatus.failed
state.error = action.payload
})
deleteApplication.fulfilled 被触发,而不是 deleteApplication.rejected。
我错过了什么吗?
【问题讨论】:
标签: reactjs redux redux-thunk