How to handle this IF node

Describe the problem/error/question

We are trying to handle this IF Node and we trying different things and it seems it’s a bug or something that we miss.

Basically, we need to handle the query result either returning value or nothing. For some reason, the output always returning TRUE if it’s blank.

What is the error message (if any)?

N/A

Please share your workflow

{
“nodes”: [
{
“parameters”: {
“operation”: “executeQuery”,
“query”: “SELECT Batch_ID, Listing_ID\nFROM dc_ai_content_moderation\nWHERE Batch_ID = {{ $json.Batch_ID }} AND Sentiment_Analysis_Category IN (‘Chemicalsss Drug Content’, ‘Gun Content’, ‘Fraud Content’) AND Sentiment_Analysis_Confidence >= 0.50”,
“options”: {}
},
“type”: “n8n-nodes-base.mySql”,
“typeVersion”: 2.4,
“position”: [
-1320,
460
],
“id”: “7800f1a7-dab6-4a35-ad34-3697008e91e5”,
“name”: “Get Non-Adult-OK Content with AI High Confidence”,
“executeOnce”: true,
“alwaysOutputData”: true,
“credentials”: {
“mySql”: {
“id”: “76mb9Wbc7PAKkPhK”,
“name”: “MySQL account”
}
}
},
{
“parameters”: {
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “loose”,
“version”: 2
},
“conditions”: [
{
“id”: “4f5dd8f0-a123-4e14-9bd2-774d3c6a23d5”,
“leftValue”: “={{ $json.debug_boolean_check }}\n\n\n\n\n\n\n\n”,
“rightValue”: true,
“operator”: {
“type”: “boolean”,
“operation”: “equals”
}
}
],
“combinator”: “or”
},
“looseTypeValidation”: true,
“options”: {}
},
“type”: “n8n-nodes-base.if”,
“typeVersion”: 2.2,
“position”: [
-920,
460
],
“id”: “86987cce-ff4d-457a-be54-744274a95ae0”,
“name”: “If1”
},
{
“parameters”: {},
“type”: “n8n-nodes-base.manualTrigger”,
“typeVersion”: 1,
“position”: [
-1760,
460
],
“id”: “0b749aa3-259a-48b3-bdb0-6d3858becc3f”,
“name”: “When clicking ‘Test workflow’”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “c29b5813-4b10-485e-aa51-c873781fe416”,
“name”: “Batch_ID”,
“value”: 82,
“type”: “number”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
-1540,
460
],
“id”: “c17f570c-8c9b-4f56-ba7c-eb3e29023fc4”,
“name”: “Edit Fields”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “1976a082-3d1e-4065-8776-c87e7e7f2a3f”,
“name”: “debug_boolean_check”,
“value”: “={{ Array.isArray($json) && $json.length > 0 && !($json.length === 1 && JSON.stringify($json[0]) === "{}") }}”,
“type”: “string”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
-1120,
460
],
“id”: “e5eb9c9b-b2a9-4344-a3d4-485ed7dad79b”,
“name”: “Edit Fields1”
}
],
“connections”: {
“Get Non-Adult-OK Content with AI High Confidence”: {
“main”: [
[
{
“node”: “Edit Fields1”,
“type”: “main”,
“index”: 0
}
]
]
},
“If1”: {
“main”: [
,

]
},
“When clicking ‘Test workflow’”: {
“main”: [
[
{
“node”: “Edit Fields”,
“type”: “main”,
“index”: 0
}
]
]
},
“Edit Fields”: {
“main”: [
[
{
“node”: “Get Non-Adult-OK Content with AI High Confidence”,
“type”: “main”,
“index”: 0
}
]
]
},
“Edit Fields1”: {
“main”: [
[
{
“node”: “If1”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“pinData”: {}
}

Your workflow:

I can’t test as I don’t have access to your SQL side of things, but the edit fields after the SQL is setting a string, but you’re comparing it with a boolean. In other words, this:

is where you’re setting it as a string, but this:

is where you’re comparing it as a boolean. Have you tried setting a boolean instead of a string?

hi @hrishikesh

We actually tried different thing. Originally, we believe we tried with the boolean and still not working.

Basically, I want to handle to handle to 2 things:

This is for empty records - it needs to FALSE

[
{
}
]

OR not empty - it needs to TRUE:

[
{
“Batch_ID”: 83,
“Listing_ID”: 483289
},
{
“Batch_ID”: 83,
“Listing_ID”: 483269
}
]

Is this an array inside an item or an empty item?
If it is an item the easiest thing is to check a field that should always exist is not empty.

Not sure if there is a default option that would directly work with your data(not at pc to check) . But what could work is checking by getting the keys like this:
Object.keys($json) and then check if that is an empty array. Or put .length after that function and check if nr is 0.

Looking at the “Edit Fields1” node expression {{ Array.isArray($json) && $json.length > 0 && !($json.length === 1 && JSON.stringify($json[0]) === '{}') }} I think I understand where the issue comes from.

$json can never be an array as it represents a non-binary data object of one item in the node’s input array (which is, in its turn, an output from the previous node).

My hypothesis is that MySQL node returns a list of meaningful records (each represented by an object and referrable to as $json) if SQL query returns any results, or a list comprising single empty object (also accessible via $json) otherwise.

If my hypothesis is correct then the following should solve your problem:

Attach an IF node to your MySQL node with a condition
{{ $input.first().json }} Object=>is not empty.
Here

  • $input refers to the entire input which is an array of entries
  • .first() returns first element from input
  • .json returns actual data we want. In your scenario this is an object representing one (out of possibly many) record or no-matching-results-situation in a form of an empty object.

Please refer to the docs on n8n peculiar data structure:

I personally do not like this solution tho (because the IF node will iterate through all records, one by one just to put them all on true output branch).

Below is a playground to try different approaches under various scenarios. My preference would be “Aggregate and cleanse records” in the playground below.
Why? As an output from the Code block we have a scoped property that would contain array of records (empty if no records are returned from the DB). To me, this is a more explicit approach vs one when an empty object represents no-records situation.

P.S. @hrishikesh @BramKn I am sorry folks, didn’t mean to steal the stage. If my hypothesis above is correct, then the incorrect work with the underlying n8n data structure is the root cause of the issue @dcpartners faces.

Thanks @Olek

Our output SQL query scenario can only return records in JSON or nothing in JSON as well.

However, we will try to follow your suggestion and will let you know.

Thanks

Hi @Olek

Redo the workflow by removing the SET node between Query and IF nodes (see below) and I got this error:

Conversion error: the string '[object Object] ’ can’t be converted to an object [condition 0, item 0]
Try changing the type of the comparison.

Hi @BramK

Can you elaborate this further please?

Actually, we managed to figure out this, it seems that the IF node has a “Convert types when required” might cause an issue so we might implement this accidently :slight_smile:

Here’s the updated one, and worked well

1 Like