[英]TypeError: Cannot read property 'push' of undefined with AngularJS
I'm trying to push my data into this CompareArray
but get the following error:我正在尝试将我的数据推送到此
CompareArray
但出现以下错误:
TypeError: Cannot read property 'push' of undefined
at Object.addResult (src\services\CompareService.js:22)
Code:代码:
this.addResult = function (type, result) {
this[type.toLowerCase() + 'CompareArray'].push(result);
this.checkToDisplayLinks(type);
this.checkToDisableCheckboxes(type);
};
Before you push anything check if it is undefined and initialized在推送任何内容之前检查它是否未定义和初始化
if(this[type.toLowerCase() + 'CompareArray']){
this[type.toLowerCase() + 'CompareArray'].push(result);
}
else
{
this[type.toLowerCase() + 'CompareArray'] = [result];
}
if you look closely, error is self descriptive.如果你仔细观察,错误是自我描述的。
Cannot read property 'push' of undefined at Object.addResult
addResult is your method. addResult 是你的方法。 You are doing "push" on
你在“推”
this[type.toLowerCase() + 'CompareArray'].push(result);
for which it says - 'push' of undefined.它说 - 未定义的“推送”。 which means
意思是
this[type.toLowerCase() + 'CompareArray']
is undefined.未定义。 Push is a method only available on arrays.
Push 是一种仅适用于数组的方法。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.