How to fix TypeError: Cannot read property 'then' of undefined in Node.js

Node.jsBEGINNERMEDIUMUpdated Dec 9, 2025· 150 views

This error occurs when attempting to chain .then() on a value that is undefined instead of a Promise. It typically happens when a function fails to return a Promise, breaking the promise chain.

Quick fix

Fixes TypeError

// Example error stack:
// TypeError: Cannot read property 'then' of undefined
//     at processData (app.js:15:8)
  1. Identify where the promise chain breaks
  2. Add return statements to promise-returning functions
  3. Return values from .then() handlers
  4. Ensure async functions return values
node.js — error
// Example error stack:
// TypeError: Cannot read property 'then' of undefined
// at processData (app.js:15:8)
TypeError: Cannot read property 'then' of undefined

What this error means

This error occurs when you try to call the `.then()` method on a value that is `undefined` instead of a Promise object. In JavaScript and Node.js, when you chain promises together, each handler in the chain is expected to either return a value or return another Promise. If a function that's supposed to return a Promise doesn't actually return anything, it implicitly returns `undefined`, and any subsequent attempt to call `.then()` on that `undefined` value will throw this TypeError. This is one of the most common mistakes when working with promise chains. The error message tells you exactly what went wrong: you're trying to access the 'then' property (which is a method on Promise objects) on something that doesn't have it—namely, `undefined`. This breaks the promise chain and prevents your asynchronous code from executing as expected.

Typical symptoms

  • TypeError appears when chaining .then() calls
  • Asynchronous operations stop executing mid-chain
  • Subsequent promise handlers never get called
  • Application crashes with "Cannot read property 'then' of undefined"
  • Promise chain breaks and catch handlers may not trigger
  • Tests fail when mocking async functions with Jest or other testing frameworks

Common causes

  1. Forgetting to return a Promise from a function that should return one
  2. Missing the return keyword before an asynchronous function call in a .then() handler
  3. A function declared as returning a Promise but not actually returning anything
  4. Not returning a value from an async function when one is expected
  5. Calling .then() on the result of a function that doesn't return a Promise
  6. Improperly chaining promises where intermediate handlers don't pass results forward
  7. Mock functions in tests not properly configured to return Promises

How to fix "TypeError: Cannot read property 'then' of undefined"

1Identify where the promise chain breaks

Examine your stack trace to find which .then() call is receiving undefined. Look for the line number in the error message.

javascript
// Example error stack:
// TypeError: Cannot read property 'then' of undefined
//     at processData (app.js:15:8)

Check the function being called just before this line. That's likely where the Promise is missing.

2Add return statements to promise-returning functions

The most common fix is adding the return keyword before your Promise or asynchronous function call.

Before (broken):

javascript
function fetchData() {
  fetch('https://api.example.com/data')
    .then(response => response.json());
  // Missing return - function implicitly returns undefined
}

// This will fail:
fetchData().then(data => console.log(data));

After (fixed):

javascript
function fetchData() {
  return fetch('https://api.example.com/data')
    .then(response => response.json());
}

// Now works correctly:
fetchData().then(data => console.log(data));
3Return values from .then() handlers

Each .then() handler in a chain must return a value or Promise if you want to continue the chain.

Before (broken):

javascript
somePromise()
  .then(result => {
    processResult(result);
    // Missing return - returns undefined
  })
  .then(data => {
    // data is undefined here!
    console.log(data);
  });

After (fixed):

javascript
somePromise()
  .then(result => {
    return processResult(result);
  })
  .then(data => {
    console.log(data); // Now data has the correct value
  });
4Ensure async functions return values

If you're using async/await, make sure async functions return the expected value or Promise.

Before (broken):

javascript
async function getData() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  // Missing return - function returns Promise<undefined>
}

getData().then(data => console.log(data)); // data is undefined

After (fixed):

javascript
async function getData() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  return data;
}

getData().then(data => console.log(data)); // Works correctly
5Fix test mocks to return Promises

When testing with Jest or other frameworks, ensure mocked async functions return Promises.

Before (broken):

javascript
// Mock doesn't return a Promise
jest.mock('./api', () => ({
  fetchUser: jest.fn(() => { userId: 123 })
}));

After (fixed):

javascript
jest.mock('./api', () => ({
  fetchUser: jest.fn(() => Promise.resolve({ userId: 123 }))
}));
6Verify the function signature and documentation

Check that the function you're calling is documented to return a Promise. Some functions may look asynchronous but don't actually return Promises.

javascript
// Check function return type
function myFunction(): Promise<Data> { // TypeScript helps here
  return fetchData();
}

Use TypeScript or JSDoc to explicitly declare return types, which helps catch these errors during development.

Advanced notes

Promise Chain Architecture:
When building complex promise chains, consider using async/await instead of .then() chains for better readability and easier debugging. Async/await makes it more obvious when you're missing a return statement because you use the await keyword explicitly.

TypeScript Benefits:
Using TypeScript with strict type checking will catch many of these errors at compile time. If a function is declared to return Promise<T> but doesn't actually return anything, TypeScript will flag it as an error.

Alternative: Promise.all() for Concurrent Operations:
If you're chaining promises sequentially but they could run in parallel, use Promise.all() instead:

javascript
// Sequential (slow):
return firstPromise()
  .then(result1 => secondPromise())
  .then(result2 => thirdPromise());

// Concurrent (fast):
return Promise.all([
  firstPromise(),
  secondPromise(),
  thirdPromise()
]);

Debugging Tips:
Add console.log statements to verify what your functions are returning:

javascript
function myFunction() {
  const promise = fetch('/api/data');
  console.log('Returning:', promise); // Should log Promise object, not undefined
  return promise;
}

Common Pattern: Implicit Returns with Arrow Functions:
Arrow functions with expression bodies implicitly return the result, which can help avoid this error:

javascript
// Explicit return (verbose):
.then(data => {
  return processData(data);
})

// Implicit return (concise):
.then(data => processData(data))

However, be careful with object literals, which need parentheses:

javascript
// Wrong - looks like a block, returns undefined:
.then(data => { value: data.value })

// Correct - returns an object:
.then(data => ({ value: data.value }))

Frequently asked questions

What does "TypeError" mean?
then() method on a value that is undefined instead of a Promise object. js, when you chain promises together, each handler in the chain is expected to either return a value or return another Promise. then() on that undefined value will throw this TypeError.
How do I fix "TypeError"?
This error occurs when attempting to chain .then() on a value that is undefined instead of a Promise. It typically happens when a function fails to return a Promise, breaking the promise chain.
What causes "TypeError"?
Forgetting to return a Promise from a function that should return one Missing the return keyword before an asynchronous function call in a .then() handler A function declared as returning a Promise but not actually returning anything
Is the "TypeError" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading