Reputation: 11
am quite new to programming and my app is returning an error when I do a forEach function. I have the following code in my 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);
}
}
});
My console is giving me this error
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)
Upvotes: 0
Views: 375
Reputation: 14699
This
$scope.ajaxRequest.$promise.then(function(){
$scope.ajaxRequest.game.forEach(function(entry) {
looks fishy. Change it to
$scope.ajaxRequest.$promise.then(function(result) {
result.forEach(function(entry) {
Now, I won't guarantee without seeing the result that it'll work, but this is the pattern of working with asynchronous calls and promises: in the .then
block, put a function that uses the output of the previous one in the promise chain as argument. So at least this change
$scope.ajaxRequest.$promise.then(function(result) {
(instead of $scope.ajaxRequest.$promise.then(function() {
) is necessary.
Upvotes: 1