Using the item method doesn't work with pinned data in this scenario. (AI Agent)

Hi. I am facing the following problem. This is the main part of my workflow for parsing web pages and finding relevant information using AI agent. I am facing a problem when I have to download excel files from a website, then I try to format it into text and pass it to the agent, but I get an error:

Using the item method doesn’t work with pinned data in this scenario. Please unpin ‘Code1’ and try again.
An expression here won’t work because it uses .item and n8n can’t figure out the matching item. You can either:
Add the missing information to the node ‘Code1’
Or use .first(), .last() or .all()[index] instead of .item

Having copied a piece that reads excel, processes it and passes it to the agent in a separate workflow I noticed that it worked there without problems. Explain to me what is my mistake in the main part.

Information on your n8n setup

  • n8n version: 1.85.1
  • Database (default: SQLite): PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system:

The issue you’re experiencing occurs because when you pin data to a node in n8n, the .item method can’t determine which specific item to reference from the pinned dataset. This is a common workflow structure issue, not a coding error.

Root Cause

When data is pinned to the ‘Code!’ node, n8n loses the execution context that normally allows .item to map to a specific data item. Your separate workflow works because it processes data in real-time without pinned data blocking the item mapping.

Solution Options

Option 1: Use Array Methods (Recommended)

Replace .item with specific array methods:

  • .first() - for the first item
  • .last() - for the last item
  • .all()[0] - for specific index (replace 0 with your desired index)

Example: Instead of {{ $json.item.fieldName }}, use {{ $json.first().fieldName }}

Option 2: Unpin and Process Dynamically

  1. Right-click the ‘Code!’ node
  2. Select “Unpin” to remove the pinned data
  3. Let the node process data dynamically from the previous node

Option 3: Reference Specific Node Output

If you need to access data from a specific previous node, use:

{{ $node["NodeName"].json.fieldName }}

Why Your Separate Workflow Works

Your working workflow processes Excel data in real-time without pinned data, allowing n8n to maintain proper item mapping throughout the execution chain.

Immediate Fix

For your current workflow, either:

  1. Unpin the ‘Code!’ node and test with live data
  2. Replace any .item references with .first() or .all()[0] depending on your data structure

This should resolve the error and allow your workflow to process Excel data properly before passing it to your AI agent.

Hi, @zacktm. Thank you for your answer!

I think I figured out exactly what my problem is. The problem is using .item in promt to my agent.

Проаналізуйте наведений нижче текст і витягніть з нього інформацію про “{{ $("Google Sheets1").item.json.indexName }}”. Мені потрібна точна за 2024 рік та прогнози на 2025, 2026 і 2027 роки. Якщо певні показники відсутні в тексті, пропускайте їх. Не використовуйте дані, яких немає в тексті.

Текст для аналізу:
{{ $json.data }}

But I used such a construction before I encountered reading excel files (no problems with reading pdf files) and everything was fine.
Let’s say I need to replace .item here with .all()[i], since .item would loop through all the items read from the google sheet.

$node[“Google Sheets1”].json.indexName - this construction takes only the first element. Can you tell me how to correctly implement the search of items from Google Sheet.

In addition, I’ve looked through my nodes several times none of them are anchored and also I didn’t see any more .item usage

Hi @mshkepast!

Great to hear you’ve identified the root cause of the issue. You’re absolutely right that .item would loop through all items, and your understanding of the problem is spot-on.

For Google Sheets Data Access:

Since $node["Google Sheets1"].json.indexName only takes the first element, here are the correct approaches:

Option 1: Access Specific Index (Recommended)

{{ $node["Google Sheets1"].json[1].indexName }}

This directly accesses the second item (index 1) from your Google Sheets data.

Option 2: Use All Data with Index

{{ $node["Google Sheets1"].json.all()[1].indexName }}

This is more explicit and works well when you need to access specific indices.

Option 3: For Dynamic Index Access

If you need to access different indices based on conditions:

{{ $node["Google Sheets1"].json[$parameter.rowIndex].indexName }}

Where rowIndex would be a parameter you set.

For Your Ukrainian Prompt Context:

Since you’re working with Google Sheets data that needs to be processed and passed to your AI agent, you can structure it like this:

// Get specific row data
{{ $node["Google Sheets1"].json[1] }}

// Or get all data and let your agent process it
{{ $node["Google Sheets1"].json.all() }}

Best Practice for Your Use Case:

Given that you’re extracting information for 2024, 2025, 2026, and 2027, you might want to:

  1. Filter the data first using a Function node to find rows matching your criteria
  2. Then pass the filtered results to your AI agent

This approach would be more efficient than accessing specific indices:

// In a Function node
const allData = $node["Google Sheets1"].json.all();
const filteredData = allData.filter(item => {
  const year = item.year; // adjust field name as needed
  return [2024, 2025, 2026, 2027].includes(year);
});
return filteredData;

This way, your AI agent gets only the relevant data, making the analysis more focused and efficient.

Does this help solve your indexing issue? Let me know if you need clarification on any of these approaches!