N
Nuxtβ€’3mo 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
danielroeβ€’3mo ago
id can also be an array of strings so it may be worth asserting it is a string
netwrx
netwrxβ€’3mo 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
Orbvioxβ€’3mo ago
whats the updated code?
danielroe
danielroeβ€’3mo ago
what's the file name and contents of the endpoint that this is fetching?
netwrx
netwrxβ€’3mo 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
danielroeβ€’3mo ago
I mean, what is the file name of the server endpoint...
netwrx
netwrxβ€’3mo ago
Oh sorry server/api/v1/stuff/fetch/[id]/index.get.ts
Flo
Floβ€’3mo ago
How should it? id can change the output. Can you try to return result: data[0] as $yourtype?
netwrx
netwrxβ€’3mo ago
what?
Flo
Floβ€’3mo 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
netwrxβ€’3mo 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
Floβ€’3mo ago
alright
netwrx
netwrxβ€’3mo ago
I appreciate the constructive feedback, just hoping danielroe has something at this point πŸ˜” @danielroe
danielroe
danielroeβ€’3mo ago
does it work if you change to ~/server/api/v1/stuff/fetch/[id].get.ts?
netwrx
netwrxβ€’3mo ago
No dice :/
danielroe
danielroeβ€’3mo ago
see if you can create a reproduction then
netwrx
netwrxβ€’3mo 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
danielroeβ€’3mo ago
You do need to rerun nuxi dev/prepare to update types. And sometimes you might need to restart your ide
Nook
Nookβ€’3mo 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
netwrxβ€’3mo 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
More Posts