反应Query Usemuont Onerror从未开火

发布于 2025-01-21 17:58:07 字数 528 浏览 0 评论 0原文

我目前正在尝试在项目中进行反应。

我在处理突变中的错误时遇到了麻烦。

在我的网络选项卡中,我可以确认服务器正在使用代码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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空心空情空意 2025-01-28 17:58:08

确保返回突变功能的结果(即axios.patch需要返回呼叫)

const { mutate } = useMutation(['mutation'], () => return 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
  },
})

Make sure to return the result of your mutation function (i.e. the axios.patch call needs to be returned)

const { mutate } = useMutation(['mutation'], () => return 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
  },
})
夏雨凉 2025-01-28 17:58:08

在发生错误之前,某些承诺可能会干扰它。示例:

.catch((error) => {
   resolve(error.response) // Must be reject(error.response)
})

CATCH应删除或编码管道以拒绝承诺。

任何try-catch块,拦截器可能会处理错误。在这种情况下,误差未返回突变。

Until the error comes to the mutation, some promises might interfere with it. Example:

.catch((error) => {
   resolve(error.response) // Must be reject(error.response)
})

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.

舟遥客 2025-01-28 17:58:08

一种简单的方法是在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 of 500. There is a
flaw in your implementation then.

夜访吸血鬼 2025-01-28 17:58:08

这可能是因为您正在按照磨损顺序进行争论。
在文档中,它说您应该按以下方式称为:

useMutation(mutationFn, {
   mutationKey,
   onError,
   onMutate,
   onSettled,
   onSuccess,
   retry,
   retryDelay,
   useErrorBoundary,
   meta,
 });

您的代码如下:

useMutation(
() => axios.patch(API_URL, params),
{
  mutationKey: 'mutation',
  onSuccess: () => {
    //this is always fired, even when response code is 400, 500, etc.
    void queryClient.invalidateQueries('query');
  },
  onError: () => {
    //should do something but never fired
  },
}

It might be because you're passing arguments in worng order.
In docs it says you should call it as follows:

useMutation(mutationFn, {
   mutationKey,
   onError,
   onMutate,
   onSettled,
   onSuccess,
   retry,
   retryDelay,
   useErrorBoundary,
   meta,
 });

Which makes your code as follows:

useMutation(
() => axios.patch(API_URL, params),
{
  mutationKey: 'mutation',
  onSuccess: () => {
    //this is always fired, even when response code is 400, 500, etc.
    void queryClient.invalidateQueries('query');
  },
  onError: () => {
    //should do something but never fired
  },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文