← Back to Error Library
TypeErrorJavaScriptJun 2026

Cannot read properties of undefined (reading 'map')

Error Message

terminal
TypeError: Cannot read properties of undefined (reading 'map')
  at UserList.vue:14:22
  at Array.forEach (<anonymous>)

Root Cause

The API response returned undefined instead of an empty array when there are no users. The component tried to call .map() before data was ready.

Fix

solution
// Before (broken)
const users = await fetchUsers()
userList.value = users.data

// After (fixed) — fallback to empty array
const users = await fetchUsers()
userList.value = users.data ?? []

Takeaway

Always guard against undefined when consuming API data. Use nullish coalescing (??) or optional chaining (?.) as a safety net.

VueAPINullish Coalescing