N
Nuxt17mo ago
Quentin

Nuxt3 : Redirect in script setup

Hi guys, I'm requesting my API in my setup, and i want to redirect to a page and stop the rest of the setup when it returns me an error. I'm using navigateTo, it's working but the console shows me errors of undefined properties on the variable i set with my API response
const { data } = await useFetch('......', {
onResponse: ({response}) => {
if (! response.ok) {
return navigateTo(.....)
}
}
});

const myVar = ref(data.value.myVar); // this one throw me an error
const { data } = await useFetch('......', {
onResponse: ({response}) => {
if (! response.ok) {
return navigateTo(.....)
}
}
});

const myVar = ref(data.value.myVar); // this one throw me an error
4 Replies
manniL
manniL17mo ago
If the response is not ok, data will not have the myVar value Even though you navigate, the flow will continue as the navigation does not block in the „onResponse“
Quentin
Quentin17mo ago
Ok, but when i do this, it's the same result. How can i do to redirect and abort the end of the setup ?
const {data} = awaitUseFetch(
...
);

if (! data.value) {
navigateTo(...)
}

const myVar = ...
const {data} = awaitUseFetch(
...
);

if (! data.value) {
navigateTo(...)
}

const myVar = ...
manniL
manniL17mo ago
you can try await navigateTo
Quentin
Quentin17mo ago
It's not working. I just tried to put await navigateTo(...); outside any function, just bellow my useFetch, and it's still the same issue, my data is null and so all the code bellow using this is in error Up ?
Want results from more Discord servers?
Add your server