[英]How to fix 'TypeError: (images || []).forEach is not a function'
[英]How to fix foreach forEach is not a function
对编程来说是很新的东西,当我执行forEach函数时,我的应用程序返回错误。 我的controller.js中有以下代码
$scope.ajaxRequest = A.Game.get({action: 'game',id: user.id});
$scope.ajaxRequest.$promise.then(function(){
$scope.ajaxRequest.game.forEach(function(entry) {
if(cards.indexOf(entry) !== -1) {
console.log('alredy in game');
} else {
if(entry.id != user.id){
cards.push(entry);
$scope.imageLocations.push(entry.photo);
}
}
});
我的控制台给我这个错误
ionic.bundle.js:26799 TypeError: $scope.ajaxRequest.game.forEach is not a function
at controllers.js:2383
at processQueue (ionic.bundle.js:29132)
at ionic.bundle.js:29148
at Scope.$eval (ionic.bundle.js:30400)
at Scope.$digest (ionic.bundle.js:30216)
at Scope.$apply (ionic.bundle.js:30508)
at done (ionic.bundle.js:24829)
at completeRequest (ionic.bundle.js:25027)
at XMLHttpRequest.requestLoaded (ionic.bundle.js:24968)
这个
$scope.ajaxRequest.$promise.then(function(){
$scope.ajaxRequest.game.forEach(function(entry) {
看起来很腥。 更改为
$scope.ajaxRequest.$promise.then(function(result) {
result.forEach(function(entry) {
现在,我无法保证不会看到它会起作用的结果,但这是使用异步调用和promises的模式:在.then
块中,将一个使用前一个输出的函数放入promise中链作为参数。 所以至少这个变化
$scope.ajaxRequest.$promise.then(function(result) {
(而不是$scope.ajaxRequest.$promise.then(function() {
)是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.