在angular.forEach上获取TypeError
Getting TypeError on angular.forEach
我对AngularJS相对陌生,我遇到了一个我不完全理解的错误。在这个代码块上,我得到了一个类型错误(如下所列)。这个错误将我指向sprint.users,它是一个用户数组。
$scope.countThisUserSprintsCompleted = function (user) {
var thisUserTotalSprintsCompleted = 0;
angular.forEach($scope.sprints, function(sprint) {
angular.forEach(sprint.users, function (sprintUser, index) {
if(sprintUser.username == user.username && sprint.status == 'complete') {
thisUserTotalSprintsCompleted++;
}
});
});
return thisUserTotalSprintsCompleted;
};
angular.js:13642 TypeError: Cannot read property 'users' of undefined
at userController.js:2123
at Object.q [as forEach] (angular.js:336)
at m.$scope.countThisUserSprintsCompleted (userController.js:2122)
at fn (eval at compile (angular.js:14539), <anonymous>:4:357)
at m.$digest (angular.js:17211)
at angular.js:17417
at e (angular.js:5912)
at angular.js:6191
这意味着,在提供的代码中,sprint
是未定义的。
由于您应该在angular.forEach
回调中获得sprint
,因此它可能是$scope.sprints
中的undefined
值。
例如:
[1, undefined, 2].forEach(function(value){
console.log(value);
});
要解决这个问题(更多的是一个补丁),您可以在使用sprint
:之前添加一个检查
[{ test: "value" }, undefined, { test: "other value" }].forEach(function(value){
if (value) console.log(value.test);
});
如果您当时没有期望undefined
值,则应在输入forEach
之前调查$scope.sprints
,并尝试解决该问题。
相关文章:
- 尝试使用.ninnerHTMLlog打印数据,获取错误Uncaught TypeError:undefined不是函数
- TypeError: 使用 Jquery JSON 获取数据时无效的 'in' 操作数 obj
- *简单*解析云代码查询失败”;TypeError:无法调用方法'获取'未定义的“检索该信息”;
- 使用Jquery rateIt插件获取未捕获的TypeError
- 在使用 MetricsGraphics.js 时获取 TypeError
- 使用 $().trigger() 并获取 TypeError: Object [object Object] 没有方法“
- 如何在javascript中声明成员函数?获取“TypeError: Object.function is not a
- 在 Javascript 中执行不区分大小写的排序并获取 TypeError: a.toLowerCase 不是一个函数
- javascript:不能遍历数组:获取:TypeError:对象不是函数
- 使用Angular.js和PHP获取TypeError
- 获取&;TypeError: undefined不是一个函数&;用函数式编程
- 获取TypeError: this.props在ReactJs上没有定义
- 获取"TypeError: document.getElementById(..)是null"当re
- 获取TypeError: Cannot set property '0'尝试向数组添加时未定义的
- 获取"TypeError: invalid 'in'操作数a"在WordPress
- 获取TypeError: undefined不是handsontable.validateCells()上的函数
- 加载jQuery脚本并获取TypeError:$.fn.appear.run不是一个函数
- 在angular.forEach上获取TypeError
- 获取TypeError:对象函数Lodash (value)
- 获取TypeError: Cannot read property 'data'当试图包含我用Angul