Can't understand .first()

I recently stumbled into the error [ERROR: Can’t determine which item to use]
It was probably caused by a switch in the workflow. In one case, I solved it using first().
AFAIK, it should get the first item from the output of a node. But the “problem node” was always outputting a single item.

So, why does first() work while item doesn’t work?

Do you get that error upon runtime? Or just when you were trying to assign the json value to a field

Because usually that error will not show upon runtime, because when you run it, it should be able to determine which item to use

Why .first() works is because now the n8n does not need to guess on which item to get, it will always takes the FIRST item no matter what

So if the data outputs 10 different item, it will only get the first one and ignore the other ones

Interesting.
I got it while editing.

So, I should go on and edit “blindly”, then execute?

If the node has always one output, can I assume the workflow will always work correctly?

And, why is this happening in the first place?
Is it because I’m using Loop Over Items before the referred node?

yes, usually it only happens on a certain time, it will not always happen

usually, when you use item and then you trigger, it will able to detect and identify which item to refer to or you can execute untill before the error node, and it should be able to identify which item to use

yes, it will always work correctly

Yes, one of the cause can be that, also if u use a ‘multiple branching’ type of node like merge node, switch node, if node, that can also happen or like 1 node but you also connect it to multiple node

1 Like

According to official documentation data mapping from n8n

When you use .item and there are multiple possible matches, n8n doesn’t know which one to use. To solve this you can either:

So, when is first() actually useful?

Hey @albertocv,
I would say use first() whenever you explicitly want to grab the first item from an output. E.g:

  • Sometimes an API returns multiple items and you are only interested in the first one. Imagine a search API that returns a list of results but you are pretty sure your query yields only one result.
  • As mentioned here, if the thread that links output items back to their source items is broken or ambiguous, and you cannot use $item
  • If, for any other reason, you are only interested in the first item. Example can be fetching weather data from OpenWeather API, where primary weather conditions are always first (and most of the time the only) element in weather array.

Hopefully this clears it up a bit, but please follow-up if you have any more questions.

2 Likes

in a case where you only need the first item, as simple as that

for example you know that a data will only always return 1 item, then using first is fine

or you know a data is returning multiple items but you only need the first item, then use first() is fine

Thanks. With “broken” do you mean that there’s a merge, if, switch or similar node that makes the flow not linear?

Yess, im pretty sure thts what he meant

1 Like

Hey guys I have a question. I ran into the same issue today. The problem is not that first and last doesn’t work for me it’s more that my node code god multiple outputs and if I use first or last it always takes only 1 output as the value instead of all. Any way to fix this?

Hi there @Mesh , if you can make a new topic or question and in there share your workflow or your code node and tag me or send me in private the link, i can definitely help you check it

its easier to track it as a question on its own rather than a comment

Hello @albertocv , do we help answer your question, if so, please mark one of our answers that helps you as the solution, as it would greatly help the community

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.