反应Query Usemuont Onerror从未开火
我目前正在尝试在项目中进行反应。
我在处理突变中的错误时遇到了麻烦。
在我的网络选项卡中,我可以确认服务器正在使用代码400或500响应,我认为这使Axios丢弃了错误,从而触发了定义的OnError函数。
但是,无论API调用如何进行,ONSUCCESS函数总是被称为。
我在这里想念什么?提前致谢。
const { mutate } = useMutation(
['mutation'],
() => axios.patch(API_URL, params),
{
onSuccess: () => {
//this is always fired, even when response code is 400, 500, etc.
void queryClient.invalidateQueries('query');
},
onError: () => {
//should do something but never fired
},
}
);
I'm currently trying out react-query in my project.
I'm having trouble with handling errors within my mutation.
In my networks tab, I can confirm that the server is responding with code 400 or 500, which I assumed makes axios throw an error, thus firing the defined onError function.
However, the onSuccess function is always called no matter how the API call goes.
What am I missing here? Thanks in advance.
const { mutate } = useMutation(
['mutation'],
() => axios.patch(API_URL, params),
{
onSuccess: () => {
//this is always fired, even when response code is 400, 500, etc.
void queryClient.invalidateQueries('query');
},
onError: () => {
//should do something but never fired
},
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保返回突变功能的结果(即
axios.patch
需要返回呼叫)Make sure to return the result of your mutation function (i.e. the
axios.patch
call needs to be returned)在发生错误之前,某些承诺可能会干扰它。示例:
CATCH
应删除或编码管道以拒绝承诺。任何
try-catch
块,拦截器可能会处理错误。在这种情况下,误差未返回突变。Until the error comes to the mutation, some promises might interfere with it. Example:
catch
pipe should be removed or coded to reject the promise.Any
try-catch
block, interceptors might handle the error. In that case, the error is not returned to the mutation.一种简单的方法是在
OnSuccess
回调中管理状态代码。这是
usemuont的错误
我也面临的错误,因此我在条件下管理状态代码。但是在500
的情况下,它应该起作用。有一个然后,您的实施中有缺陷。
A simple approach would be to manage the status code in the
onSuccess
callback.It's a bug in the
useMutation
which I am also facing so I manage the status code in conditionals. But it should work not work in case of500
. There is aflaw in your implementation then.
这可能是因为您正在按照磨损顺序进行争论。
在文档中,它说您应该按以下方式称为:
您的代码如下:
It might be because you're passing arguments in worng order.
In docs it says you should call it as follows:
Which makes your code as follows: