ERROR [vite-node] [plugin:vite:vue] [VITE_ERROR]

<script setup> const route = useRoute(); const { fetchItemById, loading, error } = useItems(); const item = ref(null); onMounted(async () => { try { item.value = await fetchItemById(route.params.id); } catch (e) { console.error('Failed to load item:', e); } }); </script> <template> .... </template> for some reason route.params.id returns undefined?
No description
2 Replies
kapa.ai
kapa.ai6d ago
Hi @MrFluffycloud. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the error you're experiencing, it seems that route.params.id is returning undefined when you're trying to access it in your component. This is likely happening because you're trying to access the route parameters in the wrong context or your route isn't properly set up. Here are some possible reasons and solutions: 1. Check your file structure and naming Make sure your file is properly named to capture the dynamic parameter. In Nuxt, dynamic routes are created by placing square brackets in the filename or directory name. For example, if you want to capture an id parameter, your file should be named something like: - pages/[id].vue or - pages/items/[id].vue Dynamic Routes documentation explains that the filename in square brackets becomes a route parameter. 2. Access route parameters at the right time The issue might be that you're trying to access route.params.id before the route is fully resolved. Instead of using it in onMounted, try using it directly in your setup:
<script setup>
const route = useRoute();
const { fetchItemById, loading, error } = useItems();
const item = ref(null);

// Access route.params.id directly in the setup
const fetchData = async () => {
try {
item.value = await fetchItemById(route.params.id);
} catch (e) {
console.error('Failed to load item:', e);
}
};

// Either call it immediately or in onMounted
onMounted(fetchData);
</script>
<script setup>
const route = useRoute();
const { fetchItemById, loading, error } = useItems();
const item = ref(null);

// Access route.params.id directly in the setup
const fetchData = async () => {
try {
item.value = await fetchItemById(route.params.id);
} catch (e) {
console.error('Failed to load item:', e);
}
};

// Either call it immediately or in onMounted
onMounted(fetchData);
</script>
3. Use a watcher to react to route changes If you're navigating between different items with the same component, you might need to watch for route changes:
<script setup>
const route = useRoute();
const { fetchItemById, loading, error } = useItems();
const item = ref(null);
const fetchData = async () => {
if (route.params.id) {
try {
item.value = await fetchItemById(route.params.id);
} catch (e) {
console.error('Failed to load item:', e);
}
}
};

// Watch for changes in the route parameter
watch(() => route.params.id, fetchData, { immediate: true });
</script>
<script setup>
const route = useRoute();
const { fetchItemById, loading, error } = useItems();
const item = ref(null);
const fetchData = async () => {
if (route.params.id) {
try {
item.value = await fetchItemById(route.params.id);
} catch (e) {
console.error('Failed to load item:', e);
}
}
};

// Watch for changes in the route parameter
watch(() => route.params.id, fetchData, { immediate: true });
</script>
4. Verify the route parameter exists You can add a check to see if the route parameter exists before trying to use it:
<script setup>
const route = useRoute();
const { fetchItemById, loading, error } = useItems();
const item = ref(null);

onMounted(async () => {
if (route.params.id) {
try {
item.value = await fetchItemById(route.params.id);
} catch (e) {
console.error('Failed to load item:', e);
}
} else {
console.warn('No ID parameter found in route');
}
});
</script>
<script setup>
const route = useRoute();
const { fetchItemById, loading, error } = useItems();
const item = ref(null);

onMounted(async () => {
if (route.params.id) {
try {
item.value = await fetchItemById(route.params.id);
} catch (e) {
console.error('Failed to load item:', e);
}
} else {
console.warn('No ID parameter found in route');
}
});
</script>
Remember that you can access route parameters using the useRoute() composable as shown in the Route Parameters documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6d ago
<@676745968867082250> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?