我对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
答案 0 :(得分:0)
这意味着,使用提供的代码,sprint
未定义。
由于您应该在sprint
回调中获得angular.forEach
,因此undefined
内的值可能为$scope.sprints
。
举个例子:
[1, undefined, 2].forEach(function(value){
console.log(value);
});
&#13;
要解决此问题(更多补丁),您可以在使用sprint
之前添加一项检查:
[{ test: "value" }, undefined, { test: "other value" }].forEach(function(value){
if (value) console.log(value.test);
});
&#13;
如果您当时没有期望undefined
值,则应在进入$scope.sprints
之前调查forEach
并尝试解决问题。