Home·Error messages

chrome · JavaScript

The message

Uncaught TypeError: Cannot read properties of undefined (reading 'name')

What it means

The value to the left of the dot is undefined and you read a property off it; the name in parentheses is the property you wanted, so the broken thing is the value before it. It comes from a response that has not arrived yet, a prop nobody passed, one level too deep as in data.user.name, or arr[0].id on an empty array. Where the value is legitimately optional, user?.name short-circuits it — but if the value was supposed to be there, ?. only converts the error into an undefined that fails on the next line, so find upstream why it is undefined. Before Chrome 78 the same error read Cannot read property 'name' of undefined.

The fix

There is no one-line command for this. The explanation says what to look at instead.

Printed by
chrome
JavaScript
15

Browser and Node errors usually tell you what broke and not why the value became what it was — you read undefined, it was not a function, it was not JSON — so the place to fix is upstream of the line that threw, and silencing just that line with ?. and default values makes the same problem reappear further away in a form that is harder to recognise.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “Uncaught TypeError: Cannot read properties of undefined (reading 'name')” mean?

The value to the left of the dot is undefined and you read a property off it; the name in parentheses is the property you wanted, so the broken thing is the value before it. It comes from a response that has not arrived yet, a prop nobody passed, one level too deep as in data.user.name, or arr[0].id on an empty array. Where the value is legitimately optional, user?.name short-circuits it — but if the value was supposed to be there, ?. only converts the error into an undefined that fails on the next line, so find upstream why it is undefined. Before Chrome 78 the same error read Cannot read property 'name' of undefined.

Q. How do I fix it?

There is no one-line command. The explanation above says what to look at instead.

Q. Which tool prints this?

chrome. It sits under JavaScript, and the message runs to 9 words.

Errors nearby