async.forEachSeries, за которым следует array.forEach - PullRequest
0 голосов
/ 29 ноября 2018

Скажем, у меня есть функция, в которой перед async.forEachSeries стоит array.forEach ().Каким будет порядок исполнения?Выполняется ли async.forEachSeries после завершения работы array.forEach ()?

function(){
  array.forEach(){ push into array something}
  async.forEachSeries(array){
    request.get(array.element)
  }

}

У меня есть следующий код, который не всегда выполняется должным образом, он выполняется большую часть времени, но иногда в таких строках, как sresult.data.push (...) он говорит, что sresult.data не определен.Наиболее вероятная причина, которую я мог понять, состоит в том, что код не выполняется последовательно и, следовательно, sresult.data не определяется при запуске.Кто-нибудь может помочь с этим?

 function (callback) {
                request.get(config.url.sectionMaster + "?parentSectionCode=" + sectionCode, function (err, result) {
                    // console.log(1)
                    // console.log(result)
                    if (err) { return next(err) }
                    if (result != undefined) {
                        result = JSON.parse(result.body)
                        let firstHeading = result.data
                        if (firstHeading != null) {
                            firstHeading.forEach(element => {
                                firstHeadingArray.push({
                                    "entityId": entityId,
                                    "finYear": finYear,
                                    "sectionCodeHead": sectionCode,
                                    "unit": unit,
                                    "sectionCodeDet": element.sectionCode,
                                    "sectionName": element.sectionName,
                                    "asOfDate": "",
                                    "childAmount": "0.00",
                                    "parentAmount": "0.00",
                                    "manualEntry": 0,
                                    "dispOrder": element.dispOrder,
                                    "AppStatus": "",
                                    "Status": "",
                                    "WF_SERIALNUMBER": "",
                                    "WF_ACTION": "",
                                    "leafNode": "",
                                    "data": []
                                })
                            });
                        } else {
                            return next("No first heading")
                        }
                    } else {
                        return next("Parent section code undefined.")
                    }
                    async.forEachSeries(firstHeadingArray, function (felement, fcallback) {
                        console.log(2)
                        request.get(config.url.sectionMaster + "?parentSectionCode=" + felement.sectionCodeDet, function (err, fresult) {
                            if (err) { return next(err) }
                            if (fresult != undefined) {
                                fresult = JSON.parse(fresult.body)
                                sheading = fresult.data
                                if (sheading != null) {
                                    sheading.forEach(function (element) {
                                        if (felement.data != undefined) {
                                            felement.data.push({
                                                "entityId": entityId,
                                                "finYear": finYear,
                                                "sectionCodeHead": sectionCode,
                                                "unit": unit,
                                                "sectionCodeDet": element.sectionCode,
                                                "sectionName": element.sectionName,
                                                "asOfDate": "",
                                                "childAmount": "0.00",
                                                "parentAmount": "0.00",
                                                "manualEntry": 0,
                                                "dispOrder": element.dispOrder,
                                                "AppStatus": "",
                                                "Status": "",
                                                "WF_SERIALNUMBER": "",
                                                "WF_ACTION": "",
                                                "leafNode": "",
                                                "data": []
                                            })
                                        } else {
                                            console.log(felement)
                                            return next({ message: "Error 1" })
                                        }
                                    })

                                } else { }
                            } else { return next({ message: "Parent section code undefined." }) }
                            async.forEachSeries(felement.data, function (selement, scallback) {
                                console.log(3)
                                request.get(config.url.sectionMaster + "?parentSectionCode=" + selement.sectionCodeDet, function (err1, sresult) {
                                    if (err1) { return next(err1) }
                                    if (sresult != undefined) {
                                        sresult = JSON.parse(sresult.body)
                                        theading = sresult.data
                                        if (theading != null) {
                                            theading.forEach(telement => {
                                                selement.data.push({
                                                    "entityId": entityId,
                                                    "finYear": finYear,
                                                    "sectionCodeHead": sectionCode,
                                                    "unit": unit,
                                                    "sectionCodeDet": telement.sectionCode,
                                                    "sectionName": telement.sectionName,
                                                    "asOfDate": "",
                                                    "childAmount": "0.00",
                                                    "parentAmount": "0.00",
                                                    "manualEntry": 0,
                                                    "dispOrder": telement.dispOrder,
                                                    "AppStatus": "",
                                                    "Status": "",
                                                    "WF_SERIALNUMBER": "",
                                                    "WF_ACTION": "",
                                                    "leafNode": "",
                                                    "data": []
                                                })
                                            });
                                        } else { }
                                    } else { return next({ message: "Parent section code undefined." }) }
                                    async.forEachSeries(selement.data, function (belement, abc) {
                                        console.log(4)
                                        request.get(config.url.sectionMaster + "?parentSectionCode=" + belement.sectionCodeDet, function (err2, bresult) {
                                            if (err2) { return next(err2) }
                                            if (bresult != undefined) {
                                                bresult = JSON.parse(bresult.body)
                                                bheading = bresult.data
                                                if (bheading != null) {
                                                    bheading.forEach(helement => {
                                                        belement.data.push({
                                                            "entityId": entityId,
                                                            "finYear": finYear,
                                                            "sectionCodeHead": sectionCode,
                                                            "unit": unit,
                                                            "sectionCodeDet": helement.sectionCode,
                                                            "sectionName": helement.sectionName,
                                                            "asOfDate": "",
                                                            "childAmount": "0.00",
                                                            "parentAmount": "0.00",
                                                            "manualEntry": 0,
                                                            "dispOrder": helement.dispOrder,
                                                            "AppStatus": "",
                                                            "Status": "",
                                                            "WF_SERIALNUMBER": "",
                                                            "WF_ACTION": "",
                                                            "leafNode": "",
                                                            "data": []
                                                        })
                                                    })
                                                }
                                            }

                                            abc()
                                        })
                                    }, function (err) {
                                        scallback()
                                    })

                                })
                            }, function (err) {
                                fcallback()
                            })
                        })
                    }, function (err) {
                        callback(err, firstHeadingArray)
                    })
                })
            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...