Guides

"is not a function" error — why it happens and how to fix it

Illustration: a hand presses a button painted on the wall, while the real lever is right nearby

The app crashes, and there's a red line in the console:

TypeError: data.map is not a function

First thought: "but .map exists, I've seen it a hundred times". Right, it exists. Just not on the thing you called it on.

Here's what this error really means. You try to press a button, and it turns out to be painted on the wall. The program reached data.map(...), looked inside data.map — and found no function there. It found undefined or something else entirely. You can't call that.

How to read this error

The message is always built the same way: what you tried to call + is not a function.

  • data.map is not a function — the problem is data: it has no map method.
  • getUser is not a function — the problem is getUser itself: at this spot it isn't a function.
  • this.props.onSave is not a function — something that should have arrived didn't.

Below the error there'll be a line number — look there. How to take error messages apart in full is covered in how to read an error message.

Next come three causes. Start with the first: it happens more often than the others combined.

Cause 1: a value of the wrong type

The most common case. You think data holds an array. But the server sent an object with an array inside:

// you expected:  [ {...}, {...} ]
// you got:       { items: [ {...}, {...} ], total: 2 }
data.map(...)   // objects have no map → error

Arrays have map, filter, forEach. Objects don't. The difference is broken down in array vs object.

How to check. Right before the failing line, add:

console.log(typeof data, Array.isArray(data), data)

If you see object false, you've got an object, not an array. If undefined, the data hasn't arrived yet or came back empty.

How to fix. Take the array from where it actually lives: data.items.map(...). And if the data might be missing, add a safety net: (data?.items ?? []).map(...). That tail means "if it's empty, use an empty list".

This whole story is a special case of a type mix-up. The full picture is in what is a data type.

Cause 2: that method doesn't exist

The second option: the method simply doesn't exist. A typo, a method from another language, or something AI made up.

The classic is contains:

"hello world".contains("world")   // TypeError: ... is not a function
"hello world".includes("world")   // true — this is the right way

In Java and C# the method is called contains; in JavaScript it's includes. Models learned from all languages at once and sometimes mix them up. It's one of the typical reasons why AI code has bugs.

How to check. Look the method up in the docs. For JavaScript that's MDN: search for "mdn String includes". If there's no method by that name, it doesn't exist.

How to fix. Replace it with a real method. Or ask AI, but be specific: "strings in JavaScript have no contains method — replace it with one that exists and check the other methods in this file against MDN".

Cause 3: the import grabbed the wrong thing

The function exists, it's in the next file over — but it's not in yours. Most often the curly braces get mixed up:

// in api.js:
export function getUser() { ... }

// in your file:
import getUser from "./api"     // ❌ looks for a default export — there isn't one
import { getUser } from "./api" // ✅ takes the function by name

Without braces, the import looks for the file's "main" export (export default). If there isn't one, getUser ends up undefined. And calling it gives you that very error.

How to check. Open the file you're importing from and see how the export is written. If it has the word default, import without braces. If not, use braces and the same name.

How to fix. Make the import match the export. One pair of braces — and it works.

If nothing helped

There's a rare fourth cause: the name got overwritten. For example, you declared a variable with the same name as a function: let total = total(). Search the file for whether that name is declared twice.

And so AI can help precisely, give it everything at once:

Weak promptis not a function error, fix it
Strong promptThe app crashes with: TypeError: data.map is not a function, line 42 in OrderList.jsx. Right before that line, console.log(typeof data, data) prints: object, and an object with fields items and total. Find where data comes from and fix the code so map is called on an array. If there is no data, show an empty list.

Why does the error show up only sometimes?

Because the data varies. The server sometimes returns an empty response or an error instead of a list. Then data is undefined — and the code crashes only in those cases.

Is it a bug in my code or in the library?

Almost always yours: the library was called the wrong way or given the wrong thing. Find the first line from your own files in the stack trace — look there.

How do I avoid this error in the future?

TypeScript helps: it flags a call to a non-existent method ahead of time, before the code runs. So does the habit of checking what actually came back from the server.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →