Your query is a classic “empty array detection” problem. It’s a common issue when working with database responses. Do you know exactly how to set up the condition properly.
You’re on the right track Supabase always returns an array, even when it’s empty : so checking for null or undefined won’t work. The trick is to check the length of the array.
A couple of easy ways to handle this in n8n:
Option 1 : If Node
In the If node, set the condition like this:
{{ $json["data"].length }}
“Greater than” → 0 → means you found a row, use the DB answer.
“Equal to” → 0 → means no match, route to your AI Agent.
Option 2 : Code Node
If you want more control, drop in a Code node after Supabase:
const results = $json.data || [];
if (results.length > 0) {
// Found a DB match → keep the answer
return [{ json: { answer: results[0].answer } }];
} else {
// Nothing found → flag it for AI
return [{ json: { fallback: true } }];
}
Then you can add an If node that checks for fallback === true and branch to your AI Agent.
That way, the workflow cleanly splits between “DB answer” and “AI answer.”
I’ve tried all the approaches mentioned (If node with $json.length, checking $json.data.length, and also using a Code node to detect results.length > 0).
The behavior is still the same:
When the message matches a row in Supabase → it replies correctly from the DB.
When the message does not match any row → instead of going to the AI Agent, the workflow still routes through the Supabase branch (empty array case).
So the “empty array detection” logic is not working for me. I always end up stuck in the DB branch, even though the HTTP Request clearly returns an empty array ([]).
It feels like I’m missing the exact n8n condition setup to properly branch on “array empty vs array with rows”.
Do you maybe have a working If node expression or an example workflow JSON that implements this fallback logic? That would help a lot