How to prevent router.query.id undefined
Unanswered
ayush_uidev posted this in #help-forum
Hi everyone i am using nextjs and for the dynamic route im using const router = useRouter() then taking the router.query.id to fetch data on client side but the problem is sometime the router.query.id is undefined and then it gets the id and fetch the data ay it takes time to load how can i prevent that ? it happen when i refresh the page
16 Replies
The first request is undefined and the second one has router.query.id
@ayush_uidev Hi everyone i am using nextjs and for the dynamic route im using const router = useRouter() then taking the router.query.id to fetch data on client side but the problem is sometime the router.query.id is undefined and then it gets the id and fetch the data ay it takes time to load how can i prevent that ? it happen when i refresh the page
What is the URL ure trying to access and what is the folder structure
@ayush_uidev Click to see attachment
Try adding parameter to the queryFn and pass the router.query.nameId from there instead
Just a wild guess
ok ill try
Does it work?
Why im dumb, that only works for useMutation
@ayush_uidev Click to see attachment
Try adding
enabled : !!router.query.nameIdThat way the queryFn will only be run if the router is not undefined
@aardani Try adding `enabled : !!router.query.nameId`
yes this is working
thanks
@ayush_uidev yes this is working
Nice. Mark the message as solved 

ok