![](/img/trans.png)
[英]Getting Uncaught TypeError: Cannot read property 'push' of undefined
[英]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
之前,只需檢查其exists
和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": []
})
}
檢查相同的
thirdelement.data
和fourthelement.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.