Bird
Raised Fist0
Vueframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between useFetch and useAsyncData in Nuxt?
easy
A. useAsyncData automatically refreshes data, useFetch never does.
B. useAsyncData requires a unique key for caching, useFetch does not.
C. useFetch is only for server-side, useAsyncData is client-side only.
D. useFetch caches data with a key, useAsyncData fetches without caching.

Solution

  1. Step 1: Understand useFetch behavior

    useFetch fetches data from a URL quickly but does not require a key for caching.
  2. Step 2: Understand useAsyncData behavior

    useAsyncData needs a unique key to cache the fetched data, allowing more control over data reuse.
  3. Final Answer:

    useAsyncData requires a unique key for caching, useFetch does not. -> Option B
  4. Quick Check:

    Key caching difference = D [OK]
Hint: Remember: useAsyncData needs a key, useFetch is simpler [OK]
Common Mistakes:
  • Thinking useFetch caches data with keys
  • Confusing client/server usage of both
  • Assuming useAsyncData auto-refreshes data
2. Which of the following is the correct syntax to use useAsyncData in a Nuxt component?
easy
A. const { data } = useAsyncData('posts', () => $fetch('/api/posts'))
B. const data = useAsyncData('/api/posts')
C. useAsyncData(() => $fetch('posts'), 'posts')
D. const { data } = useAsyncData(() => fetch('/api/posts'))

Solution

  1. Step 1: Check the required parameters for useAsyncData

    The first parameter is a unique key string, the second is a function returning a promise.
  2. Step 2: Validate each option's syntax

    const { data } = useAsyncData('posts', () => $fetch('/api/posts')) correctly uses a key 'posts' and a fetch function with $fetch. Others miss the key or use wrong order.
  3. Final Answer:

    const { data } = useAsyncData('posts', () => $fetch('/api/posts')) -> Option A
  4. Quick Check:

    Key first, fetch function second = A [OK]
Hint: Key string first, fetch function second in useAsyncData [OK]
Common Mistakes:
  • Omitting the key parameter
  • Swapping parameters order
  • Using native fetch instead of $fetch
3. Given this code snippet in a Nuxt component:
const { data, pending } = useFetch('/api/user')

return { data, pending }

What will pending be initially when the component loads?
medium
A. undefined
B. false
C. true
D. null

Solution

  1. Step 1: Understand useFetch loading state

    pending is a reactive boolean indicating if the fetch is still in progress.
  2. Step 2: Initial value of pending

    When the component loads and fetch starts, pending is true until data arrives.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Loading state pending = true [OK]
Hint: pending is true while fetching data [OK]
Common Mistakes:
  • Assuming pending is false initially
  • Confusing pending with data presence
  • Expecting pending to be null or undefined
4. You wrote this code in a Nuxt component:
const { data } = useAsyncData(() => $fetch('/api/items'))

But the data never loads. What is the likely problem?
medium
A. Using $fetch instead of native fetch.
B. Returning data directly instead of inside a function.
C. Not awaiting the useAsyncData call.
D. Missing a unique key as the first argument to useAsyncData.

Solution

  1. Step 1: Check useAsyncData parameters

    useAsyncData requires a unique key string as the first argument for caching.
  2. Step 2: Identify missing key issue

    Omitting the key causes the fetch not to run properly or cache data, so data never loads.
  3. Final Answer:

    Missing a unique key as the first argument to useAsyncData. -> Option D
  4. Quick Check:

    Key missing in useAsyncData = B [OK]
Hint: Always provide a key string first in useAsyncData [OK]
Common Mistakes:
  • Skipping the key argument
  • Confusing $fetch with fetch as cause
  • Expecting useAsyncData to be awaited
5. You want to fetch user data and posts in a Nuxt page using useAsyncData. You want posts to refresh every time the user ID changes. Which approach correctly achieves this?
hard
A. Use useAsyncData with a key including the user ID, e.g., useAsyncData(`posts-${userId}`, () => $fetch(`/api/posts?user=${userId}`)).
B. Use useAsyncData with a fixed key 'posts' and rely on automatic refresh.
C. Use useFetch instead of useAsyncData because it auto-refreshes on param change.
D. Call useAsyncData without a key and manually watch userId to refetch.

Solution

  1. Step 1: Understand caching with keys in useAsyncData

    Keys uniquely identify cached data. Changing the key triggers a new fetch.
  2. Step 2: Use dynamic key with user ID

    Including userId in the key string ensures posts refetch when userId changes.
  3. Step 3: Evaluate other options

    Fixed keys won't refresh; useFetch doesn't auto-refresh on param change; manual watch is more complex.
  4. Final Answer:

    Use useAsyncData with a key including the user ID, e.g., useAsyncData(`posts-${userId}`, () => $fetch(`/api/posts?user=${userId}`)). -> Option A
  5. Quick Check:

    Dynamic key triggers refetch = C [OK]
Hint: Include changing params in key string for refetch [OK]
Common Mistakes:
  • Using fixed keys expecting auto-refresh
  • Assuming useFetch auto-refreshes on param change
  • Skipping key causes no caching or refetch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard