N
Nuxt8mo ago
netwrx

Missing type definitions when using useFetch for a dynamic route

const route = useRoute();
const id = route.params.id;

const { data: stuff, pending: stuff_pending } = useFetch(
`/api/v1/stuff/fetch/${id}`
);
const route = useRoute();
const id = route.params.id;

const { data: stuff, pending: stuff_pending } = useFetch(
`/api/v1/stuff/fetch/${id}`
);
20 Replies
danielroe
danielroe8mo ago
id can also be an array of strings so it may be worth asserting it is a string
netwrx
netwrxOP8mo ago
I did try that as you suggested, but it didn't work so I just sent this as it was my original code
Orbviox
Orbviox8mo ago
whats the updated code?
danielroe
danielroe8mo ago
what's the file name and contents of the endpoint that this is fetching?
netwrx
netwrxOP8mo ago
pages\stuff\[id]\index.vue
// logic

setResponseStatus(event, 200);

return {
message: null,
result: data[0],
};
// logic

setResponseStatus(event, 200);

return {
message: null,
result: data[0],
};
data is an array of elements from an explicitly typed postgresql table, it works in other routes, hell even with useFetch, just not for dynamic routes.
`/.../${id as string}`
`/.../${id as string}`
danielroe
danielroe8mo ago
I mean, what is the file name of the server endpoint...
netwrx
netwrxOP8mo ago
Oh sorry server/api/v1/stuff/fetch/[id]/index.get.ts
Flo
Flo8mo ago
How should it? id can change the output. Can you try to return result: data[0] as $yourtype?
netwrx
netwrxOP8mo ago
what?
Flo
Flo8mo ago
How should typescript know what a function will return, if you change the input? Example: id 1 could return a blog post, while id 2 could return nothing
netwrx
netwrxOP8mo ago
That's not the issue, trust In other non-dynamic routes it works perfectly It has the same logic across both routes Mostly
Flo
Flo8mo ago
alright
netwrx
netwrxOP8mo ago
I appreciate the constructive feedback, just hoping danielroe has something at this point 😔 @danielroe
danielroe
danielroe8mo ago
does it work if you change to ~/server/api/v1/stuff/fetch/[id].get.ts?
netwrx
netwrxOP8mo ago
No dice :/
danielroe
danielroe8mo ago
see if you can create a reproduction then
netwrx
netwrxOP8mo ago
@danielroe Created a new project and it actually seems to work. I just for the life of me cant get it to work on my actual project Is there some sort of cache that holds types besides the .nuxt folder? That would be messing with it etc
danielroe
danielroe8mo ago
You do need to rerun nuxi dev/prepare to update types. And sometimes you might need to restart your ide
Nook
Nook8mo ago
Sorry I'm bad at discord, I miss every thread I spotted the issue and described it here: https://discord.com/channels/473401852243869706/1222581320283324446/1222581320283324446 (or is it unrelated ?)
netwrx
netwrxOP8mo ago
I recently figured out that the type definition that is being passed into result on my part was getting malformed. Maybe an issue on another module I'm using. Simply passing the array itself instead of the object fixed my specific issue I ran into this issue again, it seems really random so I ended up just explictly stating the type everywhere. useFetch<ResponseType>
Want results from more Discord servers?
Add your server