How to quickly fix the "Cannot read property 'then' of undefined" error in JavaScript
The "Cannot read property 'then' of undefined" error occurs in JavaScript when you call try then()
on a value - typically a function's return value - but this value is undefined
.
This could happen for one of the following reasons:
- Not returning the Promise in the function with
return
. - Not returning a value in an
async
function. - Not chaining Promises properly.
To fix it, ensure the function returns actually returns a Promise
.
Now let's look at some specific cases of the error and learn how to fix it in a few easy steps.
Fix: Promise not returned
The "Cannot read property 'then' of undefined" happens when you forget to use the return
keyword return the Promise
from the function.
function fetchData(apiUrl) {
// π `return` keyword missing
fetch(apiUrl).then((response) => {
return response.json();
});
}
// β Cannot read property 'then' of undefined
fetchData('/api/data')
.then((data) => console.log(data))
To fix it, simply return the Promise
with return
:
function fetchData(apiUrl) {
// we return the Promise
return fetch(apiUrl).then((response) => {
return response.json();
});
}
// β
Runs successfully
fetchData('/api/data')
.then((data) => console.log(data))
Fix: Asynchronous function doesn't return a value
The "Cannot read property 'then' of undefined" error happens in JavaScript when an async
function doesn't return a value, for example, due to an oversight on our part when writing conditional control flow.
async function getUserData(userId) {
if (userId) {
const response = await fetch(`/api/users/${userId}`);
return response.json();
}
// π No return if userId is absent
}
// β Cannot read property 'then' of undefined if userId is absent
getUserData().then(data => console.log(data));
To fix it, check all flow paths and make sure the async
function always returns a value.
async function getUserData(userId) {
if (userId) {
const response = await fetch(`/api/users/${userId}`);
return response.json();
}
// π Return a resolved Promise even if userId is absent
return Promise.resolve(null);
}
// β
Now, we can safely use 'then'
getUserData().then(data => console.log(data));
Fix: Promise
is not properly chained
The "Cannot read property 'then' of undefined" error occurs in JavaScript when you don't chain the Promise
s properly:
function fetchAndParseUser(apiUrl) {
fetch(apiUrl)
.then((response) => {
console.log(response);
// π Forgot to return the 'json' Promise
});
}
// β Error: Cannot read property 'then' of undefined
fetchAndParseUser('/api/user')
.then(data => console.log(data))
To fix it in this case, make sure that each then
in the chain returns a Promise
if we want to continue the chain.
function fetchAndParseUser(apiUrl) {
// π Here, we return the 'json' Promise
return fetch(apiUrl)
.then((response) => {
console.log(response);
return response.json(); // π Return the Promise here
});
}
// β
Now, we can safely use 'then'
fetchAndParseUser('/api/user')
.then(data => console.log(data))
See also
- How to Fix the "Cannot read property 'toString' of undefined" Error in JavaScript
- How to Fix the "Cannot read property of undefined" Error in JavaScript
- How to easily fix the "Cannot read property 'classList' of null" error in JavaScript
- How to Fix the "Cannot read Property 'push' of Undefined Error in JavaScript
- How to Fix the "Unexpected strict mode reserved word 'yield'" Error in JavaScript
- How to Fix the "Cannot Read Property 'replace' of Undefined" Error in JavaScript