-2

i want to bring data using api and i'm having trouble. there are two levels in using the api, first one is bringing basic job data and put authentication numbers in an array and second one is bringing detailed data one by one using the authentication numbers. however there are two problems. codes after the first step are not executed, and i don't know how to use 'request' module in for loop. any advice would be really helpful.

var arr = [];
var len;
var requestUrl = `${HOST}?authKey=${SERVICE_KEY}&callTp=L&returnType=XML&startPage=1&display=10`

//request job basic data
requestBasic.get(requestUrl, (err,res,body) => {
    if(err){
        console.log(`err => ${err}`)
        return;
    }
    else {
        if(res.statusCode == 200){
            var result = body
            console.log(`body data => ${result}`)
            var xmlToJson = convert.xml2json(result, {textFn:RemoveJsonTextAttribute, compact: true, spaces: 4});
            const jsonData = JSON.parse(xmlToJson);
            len = jsonData.wantedRoot.wanted.length;
            function RemoveJsonTextAttribute(value,parentElement){
                try{
                var keyNo = Object.keys(parentElement._parent).length;
                var keyName = Object.keys(parentElement._parent)[keyNo-1];
                parentElement._parent[keyName] = value;
                }
                catch(e){}
            }
            
            fs.writeFile('./jobbasic.json', xmlToJson, function(err){
                if(err) throw err;
            });            
            
            for(var i = 0; i < len; i++) {
                arr[i] =  `${HOST}?authKey=${SERVICE_KEY}&callTp=D&returnType=XML&wantedAuthNo=${jsonData.wantedRoot.wanted[i].wantedAuthNo}&infoSvc=VALIDATION`

            }
            //console.log(len)
            //console.log(`xml to json => ${xmlToJson}`)                              
        }
    }
})

//codes below here not executed, and i don't know why.
for(var i = 0; i < len; i++) {
    console.log(arr[i])
} //if i put this for loop in the requestBasic.get() above, it's executed.



for(var i = 0; i < len; i++) {
    getDetailedData(arr[i]);
}

function getDetailedData(url) {
    requestDetail.get(url, (err,res,body) => {
        if(err){
            console.log(`err => ${err}`)
        }
        else {
            if(res.statusCode == 200){
                var result = body
                console.log(`body data => ${result}`)
                var xmlToJson = convert.xml2json(result, {textFn:RemoveJsonTextAttribute, compact: true, spaces: 4});
                const jsonData = JSON.parse(xmlToJson);
                len = jsonData.wantedRoot.wanted.length;
                function RemoveJsonTextAttribute(value,parentElement){
                    try{
                    var keyNo = Object.keys(parentElement._parent).length;
                    var keyName = Object.keys(parentElement._parent)[keyNo-1];
                    parentElement._parent[keyName] = value;
                    }
                    catch(e){}
                }
                
                fs.appendFile('./jobdetail.json', xmlToJson, function(err){
                    if(err) throw err;
                });
                //console.log(len)
                //console.log(`xml to json => ${xmlToJson}`)                              
            }
        }
    })
}

this is how the result of requestBasic.get() looks like in the json file.

    "_declaration": {
        "_attributes": {
            "version": "1.0",
            "encoding": "UTF-8"
        }
    },
    "wantedRoot": {
        "total": "849",
        "startPage": "1",
        "display": "10",
        "wanted": [
            {
                "wantedAuthNo": "KF10952108130008",
                "company": "data",
                "title": "data",
                "salTpNm": "data",
                "sal": "data",
                "minSal": "data",
                "maxSal": "0",
                "region": "data",
                "holidayTpNm": "data",
                "minEdubg": "data",
                "career": "data",
                "regDt": "data",
                "closeDt": "data",
                "infoSvc": "VALIDATION",
                "wantedInfoUrl": "data",
                "wantedMobileInfoUrl": "data",                
            }, ...
stella
  • 1
  • 1
  • 1
    You need to give a lot more info, such as: (1) You say "codes after the first step are not executed" - What code *exactly* is not executed? There are no step numbers anywhere. (2) You need to show what XML data the API returns, or at least a representative and usable part of it. (3) What is `convert.xml2json()`, it is not a regular built-in function, we don't know what it does, or who wrote it, or what bugs it may have. Please **[edit] the question** and provide all these details in it. – Peter B Aug 17 '21 at 08:16
  • (1) code below where requestBasic.get() ends is not executed. i wrote comments is the code block. (3) it's a 'xml-js'module. i installed it as 'npm install xml-js' – stella Aug 17 '21 at 08:51
  • Hi, probably because len is undefined in that second loop? – LeanKhan Aug 17 '21 at 09:08
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Peter B Aug 17 '21 at 10:22

1 Answers1

0

if the first API is executed asynchronously then the code below will actually be executed first without the basic data provided by the API, and then the callback from the API will be executed, so if the loop is in the API it will show because it received the data from it.

Colin
  • 16
  • 2