I'm self learning Node-Red, and have been slowly building my way through a hobby that i might try to use at work.
The idea is I've figured out how to use the Asana API to get lists or projects and tasks.
I'm taking in the data from Asana, and want to build my own part to the msg, as msg.projects
I have an example flow of what I'm doing, using an inject node to substitute for the data from Asana. The Inject Node is formatted the same way for the function node to decode.
the problem I'm running into is trying to create an array of data. I want msg.projects to be an array with a copy of the data from the inject, with my new data that will be added in later functions.
so if you see the Project Builder debug, I've got an array built, but it seems to be using the last value of the array instead of iterating through the arrays.
I'm not sure what I'm doing wrong in my Extract Function Node.
[
{
"id": "5cdedeac905dcd4c",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "896e0b8736cb3642",
"type": "function",
"z": "5cdedeac905dcd4c",
"name": "Extract Pjt IDs",
"func": "/*\n Asana Projects Parser\n Reads the payload from the HTTP Request, parses the .data[inc]\n of the projects. Creates an Object to hold data we will need.\n This new object needs to be made for each Project gid,\n then added to msg.projects\n*/\nconst size=msg.payload.data.length //Determines the number of Projects Available\nvar arr=[]\nvar obj= {\n \"id\": null, //project id\n \"name\": null, //Name for the Project\n \"statustask\": false, //Does the Project have a task named \"Project Status\"\n \"taskid\": null, //if yes, what is the task ID?\n \"dashboard\": null //What is the Tag Associated with the Task?\n}\n\nfunction BuildProject (inc){\n obj.id = msg.payload.data[inc].gid\n obj.name = msg.payload.data[inc].name\n arr[inc] = obj\n}\n\nvar i = 0\n\nfor (i=0; i<1; i++){\n BuildProject(i)\n}\nmsg1={payload:msg.payload.data, projects:arr}\n\nreturn msg1;\n\n/*\n[\"payload.data\"][0].gid\npayload.data\n*/",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 800,
"y": 220,
"wires": [
[
"c2401bf9fc12a8b5"
]
]
},
{
"id": "c2401bf9fc12a8b5",
"type": "debug",
"z": "5cdedeac905dcd4c",
"name": "projectBuilder",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 980,
"y": 220,
"wires": []
},
{
"id": "8cae42c81dde85e2",
"type": "link in",
"z": "5cdedeac905dcd4c",
"name": "",
"links": [
"497cefeabe37dba2",
"b315f4cb17f524a3"
],
"x": 665,
"y": 220,
"wires": [
[
"896e0b8736cb3642"
]
]
},
{
"id": "23dfb575a668658c",
"type": "inject",
"z": "5cdedeac905dcd4c",
"name": "Start Test",
"props": [
{
"p": "timeclock",
"v": "",
"vt": "date"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 420,
"y": 120,
"wires": [
[
"673e92f8fd70103b"
]
]
},
{
"id": "a1636a2d504f974d",
"type": "debug",
"z": "5cdedeac905dcd4c",
"name": "Debug Injector",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 980,
"y": 120,
"wires": []
},
{
"id": "673e92f8fd70103b",
"type": "function",
"z": "5cdedeac905dcd4c",
"name": "test function format",
"func": "var msg1={payload:{\n \"data\":[]\n }\n}\n\nconst arr=[{\"gid\":\"Numb3r5\",\"name\":\"Tester 1\"},{\"gid\":\"Numb3rs\",\"name\":\"Test 2\"}]\nfor (let i in arr){\n msg1.payload.data[i]=arr[i]\n}\n\n\n//msg.payload.data=arr\nreturn msg1;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 590,
"y": 120,
"wires": [
[
"a1636a2d504f974d",
"b315f4cb17f524a3"
]
]
},
{
"id": "b315f4cb17f524a3",
"type": "link out",
"z": "5cdedeac905dcd4c",
"name": "",
"mode": "link",
"links": [
"8cae42c81dde85e2"
],
"x": 735,
"y": 80,
"wires": []
}
]