I'm following a guide where the api routes
are built like this:
1 Create server/api/route.js
file:
export default defineEventHandler((event) => { return { message: `hello api route` } })
2 Use api routing in the component as follows:
<script setup> const { data: message } = await useFetch('/api/route') </script> <template> <div> <p>api data {{ message }}</p> </div> </template>
This works, but when I try to add the query parameter
in 1.
:
export default defineEventHandler((event) => { const { name } = useQuery(event) return { message: `hello api name parameter ${name}` } })
And call it in the component2.
:
<script setup> const { data: message } = await useFetch('/api/route?name=mario') </script> <template> <div> <p>api data {{ message }}</p> </div> </template>
message
The attribute is empty. It seems useQuery(event)
produces an empty variable. Any idea why this doesn't work?
Try using
getQuery
instead ofuseQuery
useQuery(event)
is no longer supported. You can usegetQuery(event)
getQuery’s h3 document