A test looks up a book in its test data and prints the shelf code. It stops with an error.
const books = [{ title: "Dune", shelf: { code: "A3" } }];
const book = books.find((entry) => entry.title === "Emma");
console.log(book.shelf.code);
TypeError: Cannot read properties of undefined (reading 'shelf')
The language guarantees a TypeError on the last line. The sentence is how Chrome, Edge and Node word it. Firefox and Safari use other words for the same error.
Read the message in three parts.
- TypeError means a value was used in a way its kind does not allow.
- of undefined names the value on the left of a dot. Something was
undefined. - reading 'shelf' names the property the code asked for.
So the code asked for .shelf on something that was undefined. In book.shelf.code, the thing before .shelf is book. Now you know which variable to look at.
The line with the error is rarely the line with the mistake. book got its value one line earlier. find gives undefined when nothing matches, and the test data has no book called Emma.
Where undefined comes from
undefined means no value was given. You have met every source of it in this track:
- a property that does not exist on an object
- an array position past the end
- a function that has no
return - a parameter the caller left out
- a
findthat matched nothing
Where null comes from
null also means "nothing". The difference is that someone chose it. An API returns null to say it looked and found nothing. In a browser, document.querySelector gives null when no element matches. In Playwright, getAttribute gives null when the element does not have the attribute.
Reading a property of null raises the same kind of error, with "of null" in the message.
Fail early, with your own message
You can check for the missing value on the line where it appears.
const books = [{ title: "Dune", shelf: { code: "A3" } }];
const book = books.find((entry) => entry.title === "Emma");
if (book === undefined) {
throw new Error("The test data has no book titled Emma");
}
console.log(book.shelf.code);
Error: The test data has no book titled Emma
The person who reads this failure in a CI log does not need to open the code. CI means the server that runs the tests for every change. The message says what is wrong and where to fix it.
A different error: the name does not exist
A misspelled variable gives another kind of error.
const title = "Dune";
console.log(titel);
ReferenceError: titel is not defined
A ReferenceError means the name itself is unknown. The language guarantees the error kind, and the wording again comes from Chrome and Node. Compare it with the trap from the objects lecture. A misspelled variable is an error. A misspelled property is undefined, with no error.
Reading the lines under the message
An error comes with a stack trace. It is the list of function calls that were running, newest first. This one is an example of the shape, not real output:
TypeError: Cannot read properties of undefined (reading 'shelf')
at shelfCodeFor (tests/helpers/books.js:12:15)
at tests/loans.spec.js:8:20
Each line gives a file, a line number and a column. Start at the top line that is in your own code. Skip lines inside node_modules, which is the folder that holds installed packages.
Remember this
Read the error in parts: the kind, the value that was missing, and the property that was asked for. Then go back to the line where that value was created. Check for a missing value there and throw a message a teammate can act on.
Reference: undefined on MDN.