I’m using “Always output data” to ensure a Split In Batches loop doesn’t break.
However, I also need to count the output items of one of my SQL queries.
The issue with using {{$items().length}} in this case is that it counts the empty item as 1 instead of 0 (this is expected, since we are measuring by the number of items).
I tried using an IF node with the expression {{Object.keys($items()[0].json).length === 0}} so I can use a pair of SET nodes to conditionally set the count field to 0 however the names and lengths don’t align with their corresponding queries: Client A → shows length of Client B, etc.
How do I get the correct output of ‘0’ without mixing my data around? (both examples shown)
What is the error message (if any)?
Please share the workflow
Share the output returned by the last node
Information on your n8n setup
n8n version: 0.200.1
Database you’re using (default: SQLite): Postgres
Running n8n with the execution process [own(default), main]: own
Running n8n via [Docker, npm, n8n.cloud, desktop app]: K8s
You could try a .filter on the array where it’s not an empty object, or the object has > 0 keys. Something like items.filter(x => Object.keys(x).length).length
I don’t think that will work in an expression through because of the arrow function.
Hi there - I did not find a solution for the length issue but in the end I ended up solving my problem with a couple of JOIN statements in my SQL query:
SELECT ticket.number AS ticket_number, ticket.user_id, closed, ticket.created, ticket.updated, user.name, user.org_id, org.name AS org_name
FROM osti_ticket AS ticket
INNER JOIN osti_user AS user
ON user_id = user.id
INNER JOIN osti_organization AS org
ON org_id = org.id
WHERE ticket.created > '{{ $item("0").$node["Date & Time"].json["date"] }}' AND user_id = {{$json["user_id"]}}
I think the misalignment of data was happening on the second IF node because the number of records doesn’t match (in other words the name exists but the ticket item does not).