question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[useFetch] How to get the result data?

See original GitHub issue

I 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 :

image

But the unref(data) is null, So how can I get the data.value ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
wheatjscommented, Dec 14, 2021

@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 to await using something like await unil(isFinished).toBe(true) or watch the data object itself. So to access the email field you can try one of these

https://stackblitz.com/edit/vitejs-vite-avluye?file=src/App.vue

<script setup>
const { data } = useFetch('https://jsonplaceholder.typicode.com/users/1').get().json()

watch(data, (user) => {
  console.log(user.email) // or data.value.email
})
</script>

or

https://stackblitz.com/edit/vitejs-vite-golgaa?file=src%2FApp.vue

<script setup>
const { data, isFinished } = useFetch('https://jsonplaceholder.typicode.com/users/1').get().json()

await unil(isFinished).toBe(true)

console.log(data.value.email)
</script>

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.

4reactions
wheatjscommented, May 26, 2021

When you console log data it is a reference and will also update in the console when the value changes. Logging data.value will give you the value of data at the exact time you log it. Instead you can do something like. You could either use a watch to wait for the data like

const { data, isFinished } = useFetch()

watch(isFinished, () => {
  if (isFinished)
    console.log(data.value)
}):

or you can use until

const { data, isFinished } = useFetch()

await unil(isFinished).toBe(true)
console.log(data.value)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found