September 24, 2026 JavaScript / React Completed

Cannot read properties of undefined when mapping API response

The React component crashes when the API returns an empty or unexpected response. The error occurs when .map() is called on a value that is undefined instead of an array.

The goal is to safely handle the API response and prevent the component from crashing when no data is returned.

The Buggy Code

const [users, setUsers] = useState();

useEffect(() => {
  fetch("/api/users")
    .then((res) => res.json())
    .then((data) => setUsers(data.users));
}, []);

return (
  <div>
    {users.map((user) => (
      <p key={user.id}>{user.name}</p>
    ))}
  </div>
);

Submit Your Solution

Log in to submit

You must be logged in to participate in the Daily Bug challenge and earn giveaway points.

Log In

Monthly Giveaway

Every approved solution gives you an entry into our monthly Bug Finder giveaway pool.

Solve to enter

Successful Solvers