Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Need Help] 请求报错之后data被重置为undefined了 #82

Closed
npmrun opened this issue Dec 16, 2021 · 4 comments
Closed

[Need Help] 请求报错之后data被重置为undefined了 #82

npmrun opened this issue Dec 16, 2021 · 4 comments

Comments

@npmrun
Copy link

npmrun commented Dec 16, 2021

问题描述 Problem Description

const curSite = ref()
const {
    data: siteData,
    loading: siteLoading,
    error: siteError,
    refresh: siteRefresh,
    mutate: siteMutate
} = useRequest(() => http.getStruct(), {
    manual: false,
    initialData: [],
    formatResult(res) {
        return res.data || []
    }
})

如上代码,在请求报错之后,data的值重置成undefined了,我觉得设置了initialData的话就变成initialData好一点把。然后我加了下面的代码可以实现:

mutate: siteMutate
...
onError(err){
        siteMutate([])
}
@npmrun npmrun added the question Further information is requested label Dec 16, 2021
@John60676
Copy link
Member

这问题有待讨论一下

@John60676 John60676 removed the question Further information is requested label Dec 17, 2021
@npmrun
Copy link
Author

npmrun commented Dec 20, 2021

我个人认为报错了就恢复成初始化值就好了,不然我设置initialData之后又得判断一边是不是undefined,因为报错了就变成undefined了,导致别的错误

@keuby
Copy link

keuby commented Dec 21, 2021

同意,我也遇到了同样的问题,对于 data 的那个 ref,我现在一直得配合着 vueuse 的 refDefault 来避免由于接口调用失败导致的不必要的错误。

不过我认为不一定要恢复成初始值,可以开放一个 global config,用于设置错误时的值处理策略,可以有三种

  1. 设置为 undefined (默认,避免 breaking change)
  2. 设置为 initialData 值
  3. 不对 data 做处理,保持现有的值

@John60676
Copy link
Member

这个问题我们已经在 v2 版本处理了,目前 v2 版本已经发布 rc 版本。

欢迎试用。

文档:https://next.attojs.com
源码:https://github.com/AttoJS/vue-request/tree/next

安装最新版本

$ npm install vue-request@next
# or
$ yarn add vue-request@next
# or
$ pnpm install vue-request@next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants