0

I am getting question list from server with get request. Some of questions are unread and their attribute is

"okundu":"0";

I want to change it to

"okundu":"1";

In my code below I get questions from service and try to put request in loop. But it doesn't work due to async problem. I am new in angular.js .How can I solve this problem ?

$http.get('http://example/sorus?userId='+sessionStorage.refUser).success(function(response){         
        sorus=response;
        
        for(var i=0;i<sorus.length;i++){
            
            if(sorus[i].okundu=="0"){
                console.log(sorus[i]._id)
                $http.put('http://example/sorus/'+sorus[i]._id,{"okundu":"1"}).success(function(response){console.log(response)}).error(function(err){console.log("ERR : " + err)})
            }
            
        }
        
    })

in put request it always goes to error function and err returns null.If I write console.log above of put request I see that result is

59817b57a720c13e31d3def4

59817b46a720c13e31d3def3

ERR : null

ERR : null

There is async problem here. It never enters success of put request. It goes to error of put request.

Community
  • 1
  • 1
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54

1 Answers1

0

You can take the result from your post success method in a variable ($scope.itemList for example). Out of the success method of your post, you can post these variables using $resource method.

musaid53
  • 1
  • 1