【问题标题】:Getting "Uncaught TypeError: Cannot read property 'push' of undefined" sporadically偶尔收到“未捕获的类型错误:无法读取未定义的属性‘推送’”
【发布时间】:2018-12-05 14:05:25
【问题描述】:

我正在尝试从 url 获取数据并将其添加到数组中,然后在推送的数据中循环并再次调用 url 并将获取的数据推送到数组元素的“data”属性(等等等等)。它在大多数情况下都有效,但有时我会收到一个错误,即“无法在“secondelement.data.push”或“thirdelement.data.push”或“fourtheelement.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)
}

【问题讨论】:

    标签: javascript node.js api asynchronous


    【解决方案1】:

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

    array 中的push 数据之前,只需检查其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 是否相同

    【讨论】:

      【解决方案2】:

      你问题的原因是,数据不是数组,为什么它不包含数据中的 push 方法。

      在这种情况下检查是否有 secondelement.data ,thirdelement.data 作为数组或没有,如果没有做任何你想做的,让它成为数组或不要推送。

      检查值是否为数组。

      新方法

      Array.isArray(obj)
      

      旧方法

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

      【讨论】:

        猜你喜欢
        • 2017-08-12
        • 2020-07-25
        • 1970-01-01
        • 2020-08-23
        • 2018-10-19
        • 2015-10-20
        • 1970-01-01
        • 2020-01-07
        相关资源
        最近更新 更多