繁体   English   中英

偶尔收到“未捕获的TypeError:无法读取未定义的属性'push'”

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

我正在尝试从url中获取数据并将其添加到数组中,然后在推送的数据内循环,再次调用url并将提取的数据推送至array元素的“ data”属性(依此类推,依此类推) )。 它在大多数时间都有效,但是有时我会收到一个错误消息:“无法在“ secondelement.data.push”或“ thirdelement.data.push”或“ fourthelement.data.push”处随机读取未定义的属性“ push”。试图找出错误是什么,但它经常随机失败,这可能是什么帮助?

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)
}

您收到错误Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically由于data undefined或不是Array Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically出现Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically

push数据push array之前,只需检查其existsArray

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

检查相同的thirdelement.datafourthelement.data

您遇到问题的原因是,数据不是数组,因此数据中不包含push方法。

在这种情况下,请检查是否将secondelement.data,thirdelement.data作为数组,如果不执行所需操作,请将其设置为array或不推入。

检查值是否为array。

新的方法

Array.isArray(obj)

较旧的方法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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