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

handle errors gracefully when dependently fetching #37

Open
bnason opened this issue Apr 30, 2020 · 2 comments
Open

handle errors gracefully when dependently fetching #37

bnason opened this issue Apr 30, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@bnason
Copy link

bnason commented Apr 30, 2020

If I use an async fetcher, I get the following error in my console. It does actually work once the first call resolves. Is there more I should be doing, or some way to get rid of the errors?

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in getter for watcher "function () {
      return "/api/users/".concat(user.value.id, "/profile");
    }": "TypeError: Cannot read property 'id' of undefined"

I've setup a simple test project with effectively the same code as shown here https://guuu.io/2020/data-fetching-vue-composition-api/#swrv

<template>
	<div class="home">
		<div>{{ profile.firstName }} {{ profile.lastName }}</div>
		<div>{{ user.username }}</div>
	</div>
</template>

<script>
import useSWRV from 'swrv';

const fetcher = async (key) => {
	if (key === '/api/users/john') {
		return { id: 1, username: 'john' };
	} else if (key === '/api/users/1/profile') {
		return { firstName: 'John', lastName: 'Doe' };
	}
};

export default {
	name: 'UserProfile',

	props: {
		username: {
			type: String,
			required: true,
		},
	},

	setup(props) {
		const { data: user } = useSWRV(`/api/users/${props.username}`, fetcher);
		const { data: profile } = useSWRV(() => `/api/users/${user.value.id}/profile`, fetcher);

		return {
			user,
			profile,
		};
	},
};
</script>

Here is the route for completeness sake

	{
		path: '/profile/:username',
		component: () => import('../views/Profile.vue'),
		props: (route) => ({ username: route.params.username }),
	},
@darrenjennings
Copy link
Contributor

@bnason this is a known issue, unfortunately. Due to vuejs/composition-api#242, I am unable to guard against reference errors thrown in the watchers for the time being. However, you can guard against it by doing a reference check:

// https://github.com/vuejs/composition-api/issues/242
const { data: data2, error: error2 } = useSWRV(() => data1.value && `/api/profile?id=` + data1.value.id, loadProfile)
return { data1, error1, data2, error2 }

if the key resolves to false, then it will not fire the fetcher and you will not see errors. Hope this helps.

@darrenjennings
Copy link
Contributor

keeping this open until composition function or vue 3 or something we can do make this more ergonomic / fixes it

@darrenjennings darrenjennings added the enhancement New feature or request label May 1, 2020
@darrenjennings darrenjennings changed the title Error when using async fetcher handle errors gracefully when dependently fetching May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants