[useFetch] How to get the result data?
See original GitHub issueI request server data in setup()
function, this is my request:
setup() {
...
const url = ref('http://xieyezi.com:9003/mock/11/airi/index')
const { data,isFinished } = useFetch(url).get().json()
console.log('data:', data)
console.log('data.value:', unref(data))
...
}
when finish request , which data
console is :
But the unref(data)
is null, So how can I get the data.value
?
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
useFetch
Pagination With Provider. The onNewData will take the current data, and the newly fetched data, and allow you to merge the two however...
Read more >How to Fetch Data in React With A Custom useFetch Hook
In this video I will show how to make a custom useFetch hook in react. The hook allows you to fetch data from...
Read more >UseFetch Hook, Explained - Medium
The first step is to install the hook library by using one of the ... But wait, we never called this function either,...
Read more >useFetch() react hook - usehooks-ts
Here is a React Hook which aims to retrieve data on an API using the native Fetch API. I used a reducer to...
Read more >useFetch - VueUse
The useFetch function can be used by simply providing a url. The url can be either a string or a ref . The...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kissu
useFetch
is async, so you will not be able to access it immediately even if it shows up in the console when you log it. So to access it you will need toawait
using something likeawait unil(isFinished).toBe(true)
or watch the data object itself. So to access the email field you can try one of thesehttps://stackblitz.com/edit/vitejs-vite-avluye?file=src/App.vue
or
https://stackblitz.com/edit/vitejs-vite-golgaa?file=src%2FApp.vue
Using console.log can be kind of misleading in this way because it will only actually resolve the object when you expand the tree. So the data may not actually be there when you log it, but when you go to expand the tree from a console.log it will be there as it just resolved the data.
When you console log
data
it is a reference and will also update in the console when the value changes. Loggingdata.value
will give you the value ofdata
at the exact time you log it. Instead you can do something like. You could either use awatch
to wait for the data likeor you can use until