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?

2 Replies
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:
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:
4. Verify the route parameter exists
You can add a check to see if the route parameter exists before trying to use it:
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:
----------------------<@676745968867082250> Kapa.ai is still learning and improving, please let me know how I did by reacting below.