"No queryClient" error when using useQuery in async watch #53
Replies: 1 comment
-
Hi @RyanAtViceSoftware. Unfortunately the way that you try would not work, as you cannot await for the queries directly. I took a look at the repo you have provided and quickly hacked the working solution. setup() {
const router = useRouter();
const data = reactive({
router: {},
isLoading: computed(
() => postsQuery.isLoading || usersQuery.isLoading
),
isError: computed(
() => postsQuery.isError || usersQuery.isError
),
error: computed(
() => data.isError ? (postsQuery.error || usersQuery.error) : ""
),
hasData: computed(
() => !!postsQuery.data && !!usersQuery.data
),
posts: computed(
() => postsQuery && postsQuery.data
),
users: computed(
() => usersQuery && usersQuery.data
),
selectedUser: computed(
() => router?.currentRoute?.value?.query?.username
)
});
const postsKey = reactive(['posts', `userId=undefined`])
const selectedUserId = ref(undefined)
const usersQuery = reactive(useQuery("users", () => getUsers()));
const postsQuery = reactive(useQuery(postsKey ,() =>
getPosts({userid: selectedUserId.value})
));
const loadData = () => {
if (data.selectedUser && data.users) {
selectedUserId.value = data.users.find(user => user.username === data.selectedUser).id;
postsKey[1] = `userId=${selectedUserId.value}`
}
}
const changeItem = ($event) => {
router.push({ name: 'Posts', query: { username: $event.target.value }});
}
watch(router.currentRoute, loadData)
return { ...toRefs(data), changeItem };
} Let me know if you need more explanation about it. |
Beta Was this translation helpful? Give feedback.
-
I'm getting the error below
with the component showed below.
Steps to Reproduce
https://www.loom.com/share/ab4cfe92a11c4d88b8048fa2ef8cd1ea
Working Sample Repo
https://github.com/vicesoftware/vue-3-vue-query-boilerplate
Component
Beta Was this translation helpful? Give feedback.
All reactions