我有这个代码:
const deleteResponse = () => {
if (estado === 'new'){
dispatch(deleteNewResponse(id))
.then(() => {
dispatch(deleteNewVariations({id: id, token: token}))
.then(() => {
dispatch(deleteNodeWatson(id));
});
});
retId(id);
} else {
retId(id);
console.log('borrando respuesta de produccion');
}
};
这是我的slice.js:
export const deleteNewVariations = createAsyncThunk (
"delte-new-variations/delete",
async (variationsData) => {
const { id, token } = variationsData;
try {
const res = await FaqsService.deleteNewVariations(id, token);
console.log(res);
return res.data;
} catch (error) {
console.log(error.request.status);
return error.request.status;
}
});
这是在减速器内部:
[deleteNewVariations.rejected]:(state, action) => {
console.log(action.payload);
state.error = action.payload;
},
[deleteNewVariations.fulfilled]:(state, action) => {
console.log(action.payload);
state.error = action.payload;
}
使用这段代码,我试图删除数据库中三个不同表中的信息。
函数deleteResponse((始终有效。
我试图创建一个错误来停止函数(在启动函数之前手动删除了一些行(。在我的控制台中,我看到了我在后台准备的错误(404(。
但函数不会停止并完成链,也不会删除数据库中的行。
当第二次或第三次调度失败时,我不知道如何完成我的then((来停止函数。
感谢
如本响应中所述,404响应仍然是有效响应,因此dispatch()
函数仍将触发.then()
回调。要解决这个问题,只需检查请求响应状态代码是否是2xx
之类的:
const deleteResponse = () => {
if (estado === 'new') {
dispatch(deleteNewResponse(id))
.then((deleteNewRes) => {
if (deleteNewRes.ok) {
dispatch(deleteNewVariations({
id: id,
token: token
}))
.then((deleteVariationsRes) => {
if (deleteVariationsRes.ok) dispatch(deleteNodeWatson(id))
});
}
});
retId(id);
} else {
retId(id);
console.log('borrando respuesta de produccion');
}
};