1

So right now I'm working on a restful API and for this specific route I need to use data from two different existing APIs that I combine into one object array that I return on a GET route as a JSON object.

So in this case I get the upperclass from a certain API, then I get the underclass from another API based on the value from the first API. To achieve this i do a nested forEach loop but the object array that I then add into the result of the first API is nonexistent. So it feels like the part of my code that should loop through the second API call is never reached. Does anyone know what has gone wrong here? I've been looking at this problem for several days and cant figure out where I've gone wrong.

Thanks a lot for the help, I would be glad to answer your questions and expand on my problem if it's too vaguely described.

Br, Victor

function ObjectHandler(obj: any): object[] {
    const returnobject: object[] = [
    ];
    obj.forEach((e: any) => {
        const spec = e.codevalue;
        const specLC = spec.toLowerCase();
        const url: string = "http://baseurlexample.com/" + specLC;
        const underclassRAW = testfunction(url);
        const underclass: object[] = [
        ];
        underclassRAW.forEach((e: any) => {
            const metadata = e.metadata.find((e: any) => e.lang === "EN");
            const uc_keyvalues = {
                value: e.codevalue,
                explanation: metadata.name,
                description: metadata.description,
            };
            underclass.push(uc_keyvalues);
        });
        const metadata = e.metadata.find( (e: any) => e.lang === "EN");
        const keyvalues = {
            value: e.codevalue,
            explanation: metadata.name,
            underclass: underclass,
        };
        returnobject.push(keyvalues);
    });
        return returnobject;
    }



    function testfunction (URL: String): object[] {
    const value: object[] = [
    ];
    let data = "";
    https.get(URL, (resp: Response) => {
        resp.on("data", (chunk: any) => {
            data += chunk;
        });
        resp.on("end", () => {
            const newdata = JSON.parse(data);
            value.push(newdata);
        });
    })
    .on("error", (err: Error) => {
        console.log("Error: " + err.message);
    });
    return value;
    }

How the returnobject should look:

    [      
{
              "value": "A",
              "explanation": "something",
              "underclass": [
                  {
                      "value": "A1",
                      "explanation": "something",
                      "description": "something."
                  },
                  {
                      "value": "A2",
                      "explanation": "something",
                      "description": ""
                  },
                  {
                      "value": "A3",
                      "explanation": "A3",
                      "description": "something"
                  },
                  {
                      "value": "A4",
                      "explanation": "Something",
                      "description": ""
                  }
              ]
          },

How it looks:

        [      
{
              "value": "A",
              "explanation": "something",
              "underclass": []
},
madvic
  • 123
  • 2
  • 13
  • `testfunction` returns an empty array, since `http.get` completes *asynchronously*. See the linked question's answers for details and ways you address it. – T.J. Crowder Aug 10 '18 at 12:33
  • What you want is probably to use promises together with the [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) function. – Automatico Aug 10 '18 at 13:42

0 Answers0