At the moment, if I use
const response = await '$nuxt.axios.delete/account'; //pretend request
if(response) {
console.log('response', response); //logs if successful
//response is {data: "", config:{transitional:{...}, status:204, statusText:""}
}
else {
console.log('error'); //will not log if there is an error
}
there is no response at all if there is an error. I want to check if it has been successful or not, so when it is successful I can then push the object into the array, so I won’t have to make a new request to get the added piece of data.
I could use try and catch, which does return an error, but then I’d have to push the object into the array assuming it is successful, and only then delete it from the array if an error is caught. This seems a bit redundant.
Is there a way to get the response without using try catch or catch(error) ?
>Solution :
but then I’d have to push the object into the array assuming it is successful, and only then delete it from the array if an error is caught
You could instead toggle a flag then which tells you whether you got something to add, and do the actual adding afterwards.
let flag_do_not_add = false;
try {
/*request*/
} catch(e) {
flag_do_not_add = true;
}
if(!flag_do_not_add) {
/*add to array*/
}