Help build an array in function

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": []
    }
]

You are running into the fact that JS passes object by reference, so when you set obj to the arr array the obj all reference each other so when you change one all are change you will need to clone the object when you assign it to the project array.
e.g.

function BuildProject (inc){
 obj.id = msg.payload.data[inc].gid
 obj.name = msg.payload.data[inc].name
 arr[inc] = RED.util.cloneMessage(obj)
}

p.s.
Your supplied flow json is corrupt. When pasting code on the form use the </> button and paste between triple backticks.

Sorry about that. I wasn't sure how to insert code. thanks I see it now.

okay so I think I understand. The idea is my old code was essentially adding the same object into each index of the array, so when each loop, I'm just changing what the object has stored in it.

Thanks. I'll have to look up some more info on the clone message and when that's needed.

Appreciate it!

Hello,
I want to Connect Asana with Node red.
Can somebody help me?
Can i get a flow with an example?

Thanks

Henk

I can try. I'm dabbling a little with that.

have you already set up your Bearer Access Token?

that's the first step to getting any information out of Asana. I can show you some examples of how to setup an HTTP request and Function node to start using the API

Hello Shadow20X6,

I have communiction with ASANA

Now I have a lot of array 's
I don't how to manage that?
I want to write this data to a csv file

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.