Loading...
Loading...
TypeError: Cannot read properties of undefined (reading 'map')This is the most common JavaScript error. It means you tried to access a property or call a method on a value that is undefined. The value was expected to be an object or array but wasn't set yet.
The ?. operator safely short-circuits if the value is null or undefined.
// Instead of:
const names = data.users.map(u => u.name); // 💥 crashes if data.users is undefined
// Use:
const names = data?.users?.map(u => u.name) ?? [];Initialize state with an empty array or object to prevent the error during loading.
// React example
const [users, setUsers] = useState([]); // default to empty array
// Then safely map
users.map(u => <div key={u.id}>{u.name}</div>)Use our JSON Formatter to inspect the actual API response and verify the property names.