我正在尝试从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)
}
答案 0 :(得分:0)
由于Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically
是data
或不是undefined
而引起的错误Array
在
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
进行相同的检查
答案 1 :(得分:0)
出现问题的原因是,数据不是数组,因此数据中不包含push方法。
在这种情况下,请检查是否将secondelement.data,thirdelement.data作为数组,如果不执行所需操作,请将其设置为数组或不推入。
检查该值是否为数组。
新方法
Array.isArray(obj)
较旧的方法
Object.prototype.toString.call(obj) === '[object Array]';