N
Nuxt5mo ago
Furnaxe

Execute a useFetch 2 times

With the debugger, I realized that once refresh is executed in my status 200 if, the function restarts at console.log(response) and returns a success and then an error (so I enter my onResponse twice).
await useFetch('/api/articles/createArticle', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: form.value,
onResponse({response}) {
console.log(response);
if (response.status === 200) {
toast.add({
id: "success",
color: "green",
icon: "i-heroicons-check-circle",
title: t('admin.sales.success'),
description: t('admin.sales.the-yacht-created'),
});
isOpenCreate.value = false;
console.log('refresh');
resetForm();
refresh();
} else {
toast.add({
id: "error",
color: "red",
icon: "i-heroicons-x-circle",
title: t('admin.sales.an-error-occurred'),
description: t('admin.sales.please-try-again') + response._data.message,
});
}
},
});
await useFetch('/api/articles/createArticle', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: form.value,
onResponse({response}) {
console.log(response);
if (response.status === 200) {
toast.add({
id: "success",
color: "green",
icon: "i-heroicons-check-circle",
title: t('admin.sales.success'),
description: t('admin.sales.the-yacht-created'),
});
isOpenCreate.value = false;
console.log('refresh');
resetForm();
refresh();
} else {
toast.add({
id: "error",
color: "red",
icon: "i-heroicons-x-circle",
title: t('admin.sales.an-error-occurred'),
description: t('admin.sales.please-try-again') + response._data.message,
});
}
},
});
6 Replies
Furnaxe
FurnaxeOP5mo ago
Up !
oneeach
oneeach5mo ago
what is your refresh() function?
Furnaxe
FurnaxeOP5mo ago
const { data: articles, error, refresh } = useFetch(
'/api/articles/getAllArticles', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
params: {
page: page
},
});
const { data: articles, error, refresh } = useFetch(
'/api/articles/getAllArticles', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
params: {
page: page
},
});
oneeach
oneeach5mo ago
why are you calling refresh() within the useFetch? can you replicate the error here: https://stackblitz.com/
Furnaxe
FurnaxeOP5mo ago
Julien Pavoni
StackBlitz
Nuxt - Starter (forked) - StackBlitz
Create a new Nuxt project, module, layer or start from a theme with our collection of starters.
Furnaxe
FurnaxeOP5mo ago
It's a mess the code may be but I'm learning
Want results from more Discord servers?
Add your server