-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[usage] is there any forEach node ? #7
Comments
In this case you would not really need a forEach node you would need more a node which splits the array data in different entries. So the data which gets passed through the nodes is not a single object, it is an array of objects and each of it is an entry. And if add an expression on a node it will always reference the data of the current entry which gets processed. I create an example bellow to make that hopefully clearer how to achieve what you want to do. It calls also an API which returns an array. The data gets then split apart in separate entries with a Function-Node (which simply allows to execute any kind of JavaScript). So 1 entry goes into the node and 10 exit. The HTTP Request-Node which comes afterwards makes then 10 requests and each request will send different data. (it sends data to an API which simply responds with the data it receives). So currently there is no node yet which does the splitting of the entires. Still did not finish thinking about what functionality the node should also include and how it should be called. So will be kind of a Data-Transform-Node. Which should be able to do different things like splitting out data from an array (what in the example the Function-Node does) and also combine multiple entries into one in case somebody has for example 10 items and wants to send them with one call to an API. You can simply copy the data bellow, click once into the n8n window and then paste it. It will then create the nodes and connections automatically.
|
Just wanted to check-in if it did work for you or if there is anything else I can help you with. |
Hi, thank you for the example, it works well. Here are my thoughts after trying it out:
So I might not be your typical user I guess. Don't take my use cases and remarks to make your strategic decisions ;) |
Hello Thomas! Thanks a lot for your questions/feedback! I'm missing some documentation on the form messages take out of a node and into a node I'm puzzled by why in a function node item is an array if nodes are to treat messages item per item Such an application will attract users with low technical knowledge. For those users... I wonder what goals are on your roadmap and what use cases this app will target The reason I'm not using NodeRed ... I'm looking for something half-way between you app or NodeRed and Celery or Python-RQ I will not waste my time with Talend Open Studio (free raging here) I guess I would want a redis queue behind each link between nodes The example workflow for batch processing:
|
Thank you for taking the time to answer my questions and congrats for your newborn! I set up Github to watch this repo for new releases and will follow your progress. If I was to use this tool at work, the use cases would be :
|
Thanks a lot for both! Nothing to thank for. Contributors are the single most important thing for a project so if people take the time to ask good and insightful questions the least I can do is to answer them properly. About your use cases: fetch Advertising stats from [Bing Ads Api, Facebook Ads Insights api, ...], normalizing data (date format, etc) and finally uploading those to Google Analytics API read excel files (or csv, tsv) from a folder and import the data into a postgres database query a postgres view and generate a tsv/Excel file |
Created now a basic Postgres-Node and released a new version. Bellow you can find a workflow with some examples:
Hope that helps!
|
Thank you for the update. I guess we should close this issue as it's getting off topic. Maybe you could make a workflow example directory to store those handy samples 🎉 |
You are welcome. I am currently working on the n8n.io website. It will allow exactly that (at least the first version, later will allow more). People can publish there very easily different workflows. It links then automatically the used nodes so that users can also find how they can be used. |
Hi @janober, |
What error do you get? |
One example on Postgres: Query: Error if the length of array > 1 : |
Do all the items produce valid queries? Like do for example all contain the property "curTS"? |
I think should have property "curTS". table_a in MySQL:
MySQL Workflow:
Error occurred:
|
There are two problems:
The solution is to simply reorganize the nodes. You, for example, do not need "curTS" till the last node. So lets only set it there. And let's do it with a Function-Node which only returns one item as that is really all that is needed. So it would then look like this: {
"nodes": [
{
"parameters": {
"url": "https://jsonplaceholder.typicode.com/posts?userId=1",
"options": {},
"headerParametersUi": {
"parameter": []
}
},
"name": "Http Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
450,
350
]
},
{
"parameters": {
"functionCode": "const newItems = [];\nconst ts = new Date($node[\"Set\"].data[\"curTS\"]).toISOString().slice(0, 19).replace('T', ' ');\nfor (const item of $node[\"Http Request\"].data) {\n newItems.push({json: {id: item.id, title: item.title, updated_at: ts}});\n}\nreturn newItems;"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
650,
350
]
},
{
"parameters": {
"operation": "executeQuery",
"query": "=REPLACE INTO table_a (id, col1, updated_at) VALUES ({{$node[\"Function\"].data[\"id\"]}},'{{$node[\"Function\"].data[\"title\"]}}','{{$node[\"Function\"].data[\"updated_at\"]}}');"
},
"name": "MySQL",
"type": "n8n-nodes-base.mySql",
"typeVersion": 1,
"position": [
850,
350
],
"credentials": {
"mySql": ""
}
},
{
"parameters": {
"operation": "executeQuery",
"query": "=SELECT * FROM table_a WHERE updated_at <= FROM_UNIXTIME(FLOOR({{$node[\"Function1\"].data[\"curTS\"]}}/1000.0))"
},
"name": "MySQL1",
"type": "n8n-nodes-base.mySql",
"typeVersion": 1,
"position": [
1250,
350
],
"credentials": {
"mySql": ""
}
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n curTS: Date.now()\n }\n }\n];"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1050,
350
]
}
],
"connections": {
"Http Request": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
},
"Function": {
"main": [
[
{
"node": "MySQL",
"type": "main",
"index": 0
}
]
]
},
"MySQL": {
"main": [
[
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
},
"Function1": {
"main": [
[
{
"node": "MySQL1",
"type": "main",
"index": 0
}
]
]
}
}
} If you however really want to execute the last query 10x for some reason you can also specifically reference a fixed item which is now possible in the latest version as discussed in the forum here: Btw. please ask all questions always in the forum. The GitHub-Issues are only to report bugs. Thanks! |
Thanks for the quick response. |
No problem. Happy to hear that I could help! But again be aware that it will run the query 10x unless it gets changed like in the example I did send. |
I'd like to call a first HTTP request which returns a json array of objects. An then call another HTTP request for each object from that array.
Is there already a node for that use case ?
The text was updated successfully, but these errors were encountered: