简体   繁体   中英

Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically

I'm trying to get data from a url and adding it to an array, then looping inside the pushed data and again calling the url and pushing the fetched data to the "data" property of the array element (and so on and so forth). It works most of the time but sometimes I get an error that "Cannot read property "push" of undefined randomly at "secondelement.data.push" or "thirdelement.data.push" or "fourthelement.data.push". I am trying to figure out what the error is but it fails randomly ever so often. Any help as to what the problem might be ?

firstHeadingArray = []
request.get(config.url.sectionMaster + "?parentSectionCode=" + sectionCode, function (err1, firstresult) {
        if (err1) { return next(err1) }
        if (firstresult != undefined) {
            // console.log("---------------------------FIRST------------------------")
            // console.log(JSON.parse(firstresult.body))
            firstresult = JSON.parse(firstresult.body).data
            if (firstresult != null) {
                firstresult.forEach(function (firstelement) {
                    firstHeadingArray.push({
                        "entityId": entityId,
                        "finYear": finYear,
                        "sectionCodeHead": sectionCode,
                        "unit": unit,
                        "sectionCodeDet": firstelement.sectionCode,
                        "sectionName": firstelement.sectionName,
                        "asOfDate": "",
                        "childAmount": "0.00",
                        "parentAmount": "0.00",
                        "manualEntry": 0,
                        "dispOrder": firstelement.dispOrder,
                        "AppStatus": "",
                        "Status": "",
                        "WF_SERIALNUMBER": "",
                        "WF_ACTION": "",
                        "leafNode": "",
                        "data": []
                    })
                })
                async.forEachSeries(firstHeadingArray, function (secondelement, scallback) {
                    // console.log(secondelement.sectionCodeDet)
                    request.get(config.url.sectionMaster + "?parentSectionCode=" + secondelement.sectionCodeDet, function (srerr, secondresult) {
                        if (srerr) { return next(srerr) }
                        // console.log("-----------------------------------SECOND--------------------------------")
                        // console.log(JSON.parse(secondresult.body))
                        if (secondresult != undefined) {
                            // console.log(secondresult)
                            secondresult = JSON.parse(secondresult.body).data
                            if (secondresult != null) {
                                secondresult.forEach(function (secelement) {
                                    secondelement.data.push({
                                        "entityId": entityId,
                                        "finYear": finYear,
                                        "sectionCodeHead": sectionCode,
                                        "unit": unit,
                                        "sectionCodeDet": secelement.sectionCode,
                                        "sectionName": secelement.sectionName,
                                        "asOfDate": "",
                                        "childAmount": "0.00",
                                        "parentAmount": "0.00",
                                        "manualEntry": 0,
                                        "dispOrder": secelement.dispOrder,
                                        "AppStatus": "",
                                        "Status": "",
                                        "WF_SERIALNUMBER": "",
                                        "WF_ACTION": "",
                                        "leafNode": "",
                                        "data": []
                                    })
                                })

                                // console.log(secondelement)
                                async.forEachSeries(secondelement.data, function (thirdelement, tcallback) {
                                    // console.log("here")
                                    // console.log(thirdelement.sectionCodeDet)
                                    request.get(config.url.sectionMaster + "?parentSectionCode=" + thirdelement.sectionCodeDet, function (trerr, thirdresult) {
                                        // console.log("-----------------------------------------THIRD--------------------------------------")
                                        // console.log(JSON.parse(thirdresult.body))
                                        if (trerr) { return next(trerr) }
                                        if (thirdresult != undefined) {
                                            thirdresult = JSON.parse(thirdresult.body).data
                                            if (thirdresult != null) {
                                                thirdresult.forEach(function (telement) {
                                                    thirdelement.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": []
                                                    })
                                                })
                                                // console.log(thirdresult)
                                                async.forEachSeries(thirdelement.data, function (fourthelement, fcallback) {
                                                    // console.log(fourthelement.sectionCodeDet)
                                                    request.get(config.url.sectionMaster + "?parentSectionCode=" + fourthelement.sectionCodeDet, function (frerr, fourthresult) {
                                                        // console.log("------------------------FOURTH---------------------")
                                                        // console.log(JSON.parse(fourthresult.body))
                                                        if (frerr) { return next(frerr) }
                                                        if (fourthresult != undefined) {
                                                            fourthresult = JSON.parse(fourthresult.body).data
                                                            if (fourthresult != null) {
                                                                fourthresult.forEach(function (felement) {
                                                                    fourthelement.data.push({
                                                                        "entityId": entityId,
                                                                        "finYear": finYear,
                                                                        "sectionCodeHead": sectionCode,
                                                                        "unit": unit,
                                                                        "sectionCodeDet": felement.sectionCode,
                                                                        "sectionName": felement.sectionName,
                                                                        "asOfDate": "",
                                                                        "childAmount": "0.00",
                                                                        "parentAmount": "0.00",
                                                                        "manualEntry": 0,
                                                                        "dispOrder": felement.dispOrder,
                                                                        "AppStatus": "",
                                                                        "Status": "",
                                                                        "WF_SERIALNUMBER": "",
                                                                        "WF_ACTION": "",
                                                                        "leafNode": "",
                                                                        "data": []
                                                                    })
                                                                })
                                                            }
                                                            fcallback()
                                                        } else {
                                                            res.send({ message: "Something went wrong, please try again." })
                                                            return;
                                                        }
                                                    })
                                                }, function (ferr) {
                                                    // console.log("finished1")
                                                    if (ferr) { return next(ferr) }
                                                    tcallback()
                                                })
                                            } else { tcallback() }

                                        } else {
                                            res.send({ message: "Something went wrong, please try again." })
                                            return;
                                        }
                                    })
                                }, function (terr) {
                                    // console.log("finished2")
                                    if (terr) { return next(terr) }
                                    scallback()
                                })
                            } else { scallback() }
                        } else {
                            res.send({ message: "Something went wrong, please try again." })
                            return;
                        }
                    })
                },function(err){
                 res.send(firstHeadingArray)
}

You got an error Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically due to data is undefined or not an Array

Before push data in array just check its exists and Array .

if (secondelement.data && Array.isArray(secondelement.data)) {
  secondelement.data.push({
    "entityId": entityId,
    "finYear": finYear,
    "sectionCodeHead": sectionCode,
    "unit": unit,
    "sectionCodeDet": secelement.sectionCode,
    "sectionName": secelement.sectionName,
    "asOfDate": "",
    "childAmount": "0.00",
    "parentAmount": "0.00",
    "manualEntry": 0,
    "dispOrder": secelement.dispOrder,
    "AppStatus": "",
    "Status": "",
    "WF_SERIALNUMBER": "",
    "WF_ACTION": "",
    "leafNode": "",
    "data": []
  })
}

Check same for thirdelement.data and fourthelement.data

The reason to you problem is that , the data is not array that why it does not contain the push method in the data .

Check in that case if you got the secondelement.data , thirdelement.data as array or not , if not do what ever you want, make it array or don't push .

Check if the value is array .

New approach

Array.isArray(obj)

older approach

Object.prototype.toString.call(obj) === '[object Array]';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM