Merge Items after Http Request Loop

Hello guys, how are you doing?

I’m using a Merge node after a loop of HTTP Requests. The reason of this loop is the pagination of the API.

I’m trying to merge the objects coming from one request with the objects coming from the request loop. Everything works fine, except the merge node, that stops after the first loop.

How can I handle this situation?

Best regards

Hi @BudgtMe_App

Seems to me like you need to add all the executions together before going to the merge. this can be done like this. Please note you need to check and finish the workflow before you can use it. :wink:

Hello @BramKn,

It worked! Thanks for your help.

Now, the problem is on the Merge Transaction with Account node: The input 1 receives 6 accounts and the input 2 receives 1325 transactions. The output should be 1325 transactions enriched by its accounts, but I only receive the last 12 transactions. I don’t know why it’s happening.

Do you have any ideas?

Hi @BudgtMe_App

I think the merge isn’t set up right.
You will need to merge by key and also make sure the transactions are in the first input not in the second.
You will then get all transactions with data from accounts if available.

It worked!

Thanks

1 Like

Hello, how can I see your solution ? I just see the thread but no code or screenshots, any idea why ?

Hi @Vincent_Wasteels

It is a very old topic, so please create a new topic with your question.
I have no idea why the solution is not shown anymore.

@Vincent_Wasteels :wave: We’re looking into why the previews aren’t showing for some, but here’s the JSON code mentioned. If you’re still running into issues, please create a new topic :+1:

{
  "meta": {
    "instanceId": "78d825d87f785724cf43630a4288f440c19503b47b2cc3ea56a86f39cc755ff3"
  },
  "nodes": [
    {
      "parameters": {
        "operation": "insert",
        "collection": "accounts",
        "fields": "=_id, item, name, marketingName, currencyCode, balance, type, subtype, number, taxNumber, owner, bankData, creditData,",
        "options": {
          "dateFields": "",
          "useDotNotation": true
        }
      },
      "name": "Save Accounts to DB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1640,
        620
      ],
      "id": "b1a432b2-e715-4f10-a615-bcf6282fe450",
      "credentials": {
        "mongoDb": {
          "id": "1",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "results",
        "options": {}
      },
      "name": "Accounts List",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        720,
        600
      ],
      "id": "62515fd9-21d5-40f4-bafc-836cff7604d2"
    },
    {
      "parameters": {
        "keys": {
          "key": [
            {
              "currentKey": "id",
              "newKey": "_id"
            }
          ]
        },
        "additionalOptions": {
          "regexReplacement": {
            "replacements": [
              {
                "searchRegex": "=",
                "options": {}
              }
            ]
          }
        }
      },
      "name": "Rename Account Id",
      "type": "n8n-nodes-base.renameKeys",
      "typeVersion": 1,
      "position": [
        940,
        600
      ],
      "id": "24a74a6b-c4bb-442b-813a-45fb8150eb57"
    },
    {
      "parameters": {
        "keys": {
          "key": [
            {
              "currentKey": "id",
              "newKey": "_id"
            }
          ]
        },
        "additionalOptions": {}
      },
      "name": "Rename Transaction Id",
      "type": "n8n-nodes-base.renameKeys",
      "typeVersion": 1,
      "position": [
        1880,
        840
      ],
      "id": "62c8ae10-8fc7-42dc-b8b4-08159cb6a2ec"
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "transactions",
        "fields": "=_id, accountId, date, category, currencyCode, amount, description, descriptionRaw, status, type, creditCardMetadata, paymentData, providerCode",
        "options": {
          "dateFields": ""
        }
      },
      "name": "Save Transactions to DB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        2600,
        860
      ],
      "id": "e0b11903-f1ba-4641-9ece-143052813fc3",
      "credentials": {
        "mongoDb": {
          "id": "1",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "keys": {
          "key": [
            {
              "currentKey": "id",
              "newKey": "_id"
            }
          ]
        },
        "additionalOptions": {}
      },
      "name": "Rename Item Id",
      "type": "n8n-nodes-base.renameKeys",
      "typeVersion": 1,
      "position": [
        720,
        360
      ],
      "id": "03d21844-f1c7-4ded-9039-8f007b3457f6"
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "items",
        "fields": "=connector, _id, clientUserId, error, executionStatus, parameter, status, statusDetail, webhookUrl, createdAt, lastUpdatedAt, updatedAt",
        "options": {
          "dateFields": "=",
          "useDotNotation": true
        }
      },
      "name": "Save Item to DB",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        940,
        360
      ],
      "id": "41f99fca-9cfb-47dc-b1dc-b473eb7beb0b",
      "credentials": {
        "mongoDb": {
          "id": "1",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "operation": "removeDuplicates",
        "compare": "selectedFields",
        "fieldsToCompare": {
          "fields": [
            {
              "fieldName": "results.accountId"
            }
          ]
        },
        "options": {
          "removeOtherFields": false
        }
      },
      "name": "Unique Accounts with Transactions",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1400,
        1060
      ],
      "id": "f3d2554f-c6f2-452c-a3ba-ea4c562870f3"
    },
    {
      "parameters": {
        "fieldToSplitOut": "results",
        "include": "allOtherFields",
        "options": {}
      },
      "name": "Transactions List with Pages",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1200,
        1060
      ],
      "id": "454ac762-c500-403c-9ca1-11687f65bac4"
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"page\"]}}",
              "value2": "={{$json[\"totalPages\"]}}"
            }
          ]
        }
      },
      "name": "More Pages?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1640,
        1060
      ],
      "id": "a04a66e5-54bc-4a32-a286-b3196fa52b6f"
    },
    {
      "parameters": {
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "url": "https://xxxxx",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "pageSize",
              "value": "50"
            },
            {
              "name": "accountId",
              "value": "={{$json[\"id\"]}}"
            }
          ]
        }
      },
      "name": "Get Transactions First Page",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        940,
        840
      ],
      "id": "236123c3-35dc-482d-8a0c-727393f3c11a",
      "credentials": {
        "httpHeaderAuth": {
          "id": "3",
          "name": "Clockify"
        }
      }
    },
    {
      "parameters": {
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "url": "https://xxxxx",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "accountId",
              "value": "={{$json[\"results\"][\"accountId\"]}}"
            },
            {
              "name": "page",
              "value": "={{parseInt($json[\"page\"]) + 1}}"
            },
            {
              "name": "pageSize",
              "value": "50"
            }
          ]
        }
      },
      "name": "Get Transactions Next Pages",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1880,
        1040
      ],
      "id": "259c7ba3-7807-407f-88ed-1a17f7cf16ee",
      "credentials": {
        "httpHeaderAuth": {
          "id": "3",
          "name": "Clockify"
        }
      }
    },
    {
      "parameters": {
        "operation": "aggregateItems",
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "results"
            }
          ]
        },
        "options": {
          "disableDotNotation": false
        }
      },
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1400,
        840
      ],
      "id": "b73251a6-5dfc-4a51-b33a-8930cecedd82",
      "disabled": true
    },
    {
      "parameters": {
        "fieldToSplitOut": "results",
        "options": {
          "disableDotNotation": false
        }
      },
      "name": "Final Transactions List",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1640,
        840
      ],
      "id": "cfd5674d-e36b-42ab-9fb3-627e83e6d704",
      "disabled": true
    },
    {
      "parameters": {
        "mode": "mergeByKey",
        "propertyName1": "itemId",
        "propertyName2": "_id"
      },
      "name": "Merge Item with Account",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1180,
        620
      ],
      "id": "07ec3b4a-8df9-4791-a75a-f7510c8cf2fb"
    },
    {
      "parameters": {
        "functionCode": "item.item = {\n  _id: item.itemId,\n  connector: item.connector,\n  createdAt: item.createdAt,\n  updatedAt: item.updatedAt,\n  status: item.status,\n  executionStatus: item.executionStatus,\n  lastUpdatedAt: item.lastUpdatedAt,\n  webhookURL: item.webhookUrl,\n  error: item.error,\n  clientUserId: item.clientUserId,\n  statusDetail: item.statusDetail,\n  parameter: item.parameter\n}\n\ndelete item.itemId\ndelete item.connector\ndelete item.createdAt\ndelete item.updatedAt\ndelete item.status\ndelete item.executionStatus\ndelete item.lastUpdatedAt\ndelete item.webhookUrl\ndelete item.error\ndelete item.clientUserId\ndelete item.statusDetail\ndelete item.parameter\n\nreturn item;"
      },
      "name": "Format Account Object",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        1400,
        620
      ],
      "id": "a0c0172a-d3af-401f-add9-d0f3e931046e"
    },
    {
      "parameters": {
        "mode": "keepKeyMatches",
        "propertyName1": "accountId",
        "propertyName2": "_id"
      },
      "name": "Merge Transaction with Account",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        2120,
        860
      ],
      "id": "3ed6f8fc-21fb-4365-b04a-d2212e8282db"
    },
    {
      "parameters": {
        "functionCode": "item.account = {\n  _id: item.accountId,\n  subtype: item.subtype,\n  name: item.name,\n  itemId: item.itemId,\n  number: item.number,\n  marketingName: item.marketingName,\n  taxNumber: item.taxNumber,\n  owner: item.owner,\n  bankData: item.bankData,\n  creditData: item.creditData,\n  item: item.item\n}\n\ndelete item.accountId\ndelete item.subtype\ndelete item.name\ndelete item.itemId\ndelete item.number\ndelete item.marketingName\ndelete item.taxNumber\ndelete item.owner\ndelete item.bankData\ndelete item.creditData\ndelete item.item\n\nreturn item;"
      },
      "name": "Format Account Object1",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        2340,
        860
      ],
      "id": "708f435e-29dc-4549-8192-e94e6272061f"
    },
    {
      "parameters": {
        "functionCode": "const allData = []\n\nlet counter = 0;\ndo {\n  try {\n    const items = $items(\"Transactions List with Pages\", 0, counter).map(item => item.json);\n    allData.push.apply(allData, items);\n  } catch (error) {\n    return [{json: {allData}}];  \n  }\n\n  counter++;\n} while(true);\n\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1880,
        1280
      ],
      "id": "af216c8c-ee56-4774-acbc-4118b472cc6e"
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "11f9f44a-fa4c-4636-87d6-d10669d0cf69",
      "name": "To Do",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        2080,
        1280
      ]
    }
  ],
  "connections": {
    "Accounts List": {
      "main": [
        [
          {
            "node": "Get Transactions First Page",
            "type": "main",
            "index": 0
          },
          {
            "node": "Rename Account Id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rename Account Id": {
      "main": [
        [
          {
            "node": "Merge Item with Account",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rename Transaction Id": {
      "main": [
        [
          {
            "node": "Merge Transaction with Account",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rename Item Id": {
      "main": [
        [
          {
            "node": "Save Item to DB",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge Item with Account",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Unique Accounts with Transactions": {
      "main": [
        [
          {
            "node": "More Pages?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transactions List with Pages": {
      "main": [
        [
          {
            "node": "Unique Accounts with Transactions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "More Pages?": {
      "main": [
        [
          {
            "node": "Get Transactions Next Pages",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Transactions First Page": {
      "main": [
        [
          {
            "node": "Transactions List with Pages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Transactions Next Pages": {
      "main": [
        [
          {
            "node": "Transactions List with Pages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Final Transactions List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Item with Account": {
      "main": [
        [
          {
            "node": "Format Account Object",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Account Object": {
      "main": [
        [
          {
            "node": "Save Accounts to DB",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge Transaction with Account",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Transaction with Account": {
      "main": [
        [
          {
            "node": "Format Account Object1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "To Do",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "To Do": {
      "main": [
        [
          {
            "node": "Rename Transaction Id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
``
2 Likes

Thanks for your answer !

2 Likes