Cannot read properties of undefined (reading 'map')

I’m getting this error when I try running of my agents. Other agents within the same workflow don’t have this error

{
“errorMessage”: “Cannot read properties of undefined (reading ‘map’)”,
“errorDetails”: {},
“n8nDetails”: {
“time”: “7/29/2025, 8:08:35 AM”,
“n8nVersion”: “1.86.1 (Self Hosted)”,
“binaryDataMode”: “default”,
“cause”: {}
}
}

Hey Derrick,

It seems to me like you might want to add a validation.
You can perform this by checking if the variable is defined and is an array before calling .map()

const arr = $json["yourField"];
if (!arr || !Array.isArray(arr)) {
  return [];
}
return arr.map(...);

A simple function nodes might also assist here. You can try to something like this

const result = ($json["yourField"] || []).map(...);

Ref to these links:

Hi,

I ran into the same issue, and after some digging, I found the cause and a workaround.

The error is due to a malformed message in the chat history — specifically, a record like this:

{
  "type": "human",
  "additional_kwargs": {},
  "response_metadata": {}
}

This entry is missing a content field, and when the AI Agent node tries to read it, it throws the Cannot read properties of undefined (reading 'map') error.

To fix it, I wrote an SQL statement that removes these problematic entries from the n8n_chat_histories table:

Just replace 'YOUR_SESSION_ID' with the actual session ID where the issue occurs. After running this and re-executing the AI Agent node, everything worked fine again.

If you’re going to use it like my image, make sure you enable the workflow timeout, so you don’t end up in an infinite loop if that’s not the problem.

Hope this helps! Let me know if you need any further details.

4 Likes

For local chat, this often happens for me when changing from a standard AI model to a thinking AI model. This changes a json.output to an array, and if returning the output to chat history, the array winds up in the chat history rather than the regular output.

Fix: On local chat history (not PostGres or other), just increment the name to start a new chat. ie. Chat1 to Chat2.

I handle actual history through a memory database, and only a local chat for local context, so this usually does not impact anything for me (not serving chat to hundreds of users, just myself on the backend).

I am having the same issue, it is so frustrating that it works for my Enquiry Agent. However, it sometimes works and sometimes doesn’t work for the Booking Agent. Can anyone please help?

I found this Error "Cannot read properties of undefined (reading 'map')" generating image using Gemini · Issue #17809 · n8n-io/n8n · GitHub , seems like it is a bug.

Hey, to anyone still having this issue with postgres chat memory nodes, modifying the table where the records are being store, allowing nulls on the jsonb field fixed it for me