0
0
Vueframework~20 mins

Nuxt data fetching (useFetch, useAsyncData) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nuxt Data Fetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Nuxt component using useFetch?

Consider this Nuxt 3 component that fetches user data:

const { data, pending, error } = useFetch('/api/user')

return () => {
  if (pending.value) return 'Loading...'
  if (error.value) return 'Error!'
  return `User: ${data.value.name}`
}

Assuming the API returns { name: 'Alice' }, what will the rendered output be?

Vue
const { data, pending, error } = useFetch('/api/user')

return () => {
  if (pending.value) return 'Loading...'
  if (error.value) return 'Error!'
  return `User: ${data.value.name}`
}
AUser: Alice
BLoading...
CUser: undefined
DError!
Attempts:
2 left
💡 Hint

Think about what useFetch returns and when the component renders.

state_output
intermediate
2:00remaining
What is the value of data after useAsyncData resolves?

Given this Nuxt 3 setup:

const { data, error } = await useAsyncData('post', () => $fetch('/api/post/1'))

If the API returns { id: 1, title: 'Hello' }, what is data.value?

Vue
const { data, error } = await useAsyncData('post', () => $fetch('/api/post/1'))
Anull
Bundefined
C{ id: 1, title: 'Hello' }
DError object
Attempts:
2 left
💡 Hint

useAsyncData resolves with the fetched data inside data.value.

📝 Syntax
advanced
2:00remaining
Which option correctly uses useFetch with error handling in Nuxt 3?

Choose the code snippet that correctly fetches data and handles errors using useFetch in a Nuxt 3 setup.

A
const { data, error } = useFetch('/api/data')
if (error.value) console.error(error.value)
B
const { data, error } = useFetch('/api/data')
if (error) console.error(error)
C
const { data, error } = await useFetch('/api/data')
if (error.value) console.error(error.value)
D
const { data, error } = useFetch('/api/data')
if (error.value) console.log(data.value)
Attempts:
2 left
💡 Hint

Remember that error is a ref and must be accessed with .value.

🔧 Debug
advanced
2:00remaining
Why does this useAsyncData call cause a runtime error?

Examine this code snippet:

const { data } = useAsyncData(() => fetch('/api/items').then(res => res.json()))
console.log(data.value.length)

What causes the runtime error?

Vue
const { data } = useAsyncData(() => fetch('/api/items').then(res => res.json()))
console.log(data.value.length)
AThe <code>then</code> callback returns a promise instead of data
B<code>data.value</code> is undefined because <code>useAsyncData</code> is asynchronous and data is not ready yet
CThe fetch URL is incorrect causing a network error
DThe <code>console.log</code> is inside the async callback causing a syntax error
Attempts:
2 left
💡 Hint

Think about when data.value is available after calling useAsyncData.

🧠 Conceptual
expert
2:00remaining
Which statement about useFetch vs useAsyncData in Nuxt 3 is correct?

Choose the correct statement about the differences between useFetch and useAsyncData in Nuxt 3.

A<code>useAsyncData</code> returns raw promises, <code>useFetch</code> returns refs
B<code>useFetch</code> automatically serializes and deserializes JSON, while <code>useAsyncData</code> requires manual parsing
C<code>useFetch</code> can only be used on client side, <code>useAsyncData</code> only on server side
D<code>useAsyncData</code> caches data by key and supports server-side rendering, <code>useFetch</code> does not cache by default
Attempts:
2 left
💡 Hint

Think about caching and SSR support differences.