I have a "composable" axios function like this
export const useFetch = (url: string, config: any = {}) => {
const data = ref(null)
const status = ref()
const error = ref(null)
const loading = ref(false)
const fetch = async () => {
loading.value = true
error.value = null
try {
const result = await service.request({
url,
...config
})
status.value = result.status
data.value = result.data
} catch (ex) {
error.value = ex
} finally {
loading.value = false
}
}
fetch()
return { status, error, data, loading }
}
In a separate file like user.vue, I call useFetch like this
setup() {
const { data, error } = useFetch(
'https://jsonplaceholder.typicode.com/po3sts/1',
{
method: 'get'
}
)
console.error(error)
console.error(error.value)
return { data, error }
}
My problem is when I console.error(error), I can clearly see
Object { value: Getter & Setter }
value: Error: Request failed with status code 404
But if I do console.error(error.value), it returns null.
Any tips on how to get error.value?