-1

I am writing a simple java script function inside a controller of angularJS,

$scope.getwork = function(taskData){

    $scope.workData = workData;
    var i =0;
    do{
        var getworkQuery = CommunicationsService.getGetworkValue($scope.workData.requestId, $scope.workData.waitTime);
        getworkQuery.query({},function(resp1,$scope){
            $scope.getTaskValue=resp1;
            console.log("inside getTaskAttri ");
            console.log($scope.getTaskValue);
            console.log($scope.getTaskValue.status);
            console.log($scope.getTaskValue.errorMessage);
            i++;
        });
    }while($scope.getworkValue.status == 'processing' && i<3);

}

Here, I am getting the error,

TypeError: Cannot read property 'status' of undefined
    at Object.$scope.getTaskAttri (http://127.0.0.1:7002/vtmui/views/js/Controllers/GetWorkController.js:128:31)
    at http://127.0.0.1:7002/vtmui/views/js/Controllers/GetTaskController.js:102:24
    at http://127.0.0.1:7002/vtmui/views/lib/angular/angular-resource.js:413:30
    at wrappedCallback (http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:6995:59)
    at http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:7032:26
    at Object.Scope.$eval (http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:8218:28)
    at Object.Scope.$digest (http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:8077:25)
    at Object.Scope.$apply (http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:8304:24)
    at done (http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:9357:20)
    at completeRequest (http://127.0.0.1:7002/vtmui/views/lib/angular/angular.js:9531:7) angular.js:5930
inside getTaskAttri  GetTaskController.js:122
Resource {requestId: "2641991", status: "failure", errorMessage: "Communication failure during execution process.", waitTime: 0, workLsit: Array[0]…}
failure 
Communication failure during execution process.

console.log inside the do..while loop is getting displayed but it is showing undefined error in the while loop for the variable $scope.getworkValue.status:

while($scope.getworkValue.status == 'processing' && i<3);

Any idea how to solve this?

sbala_20
  • 95
  • 2
  • 4
  • 10
  • 5
    Welcome to the wonderful world of **async**! You can't do that. – SLaks Dec 10 '13 at 22:20
  • 2
    See if this helps http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs Dec 10 '13 at 22:20
  • @Slaks I understood the aysnc function. Whether there is any way to achieve my requirement. I need to make an ajax call until the $scope.getworkValue.status='processing' or maximum of 3 times. Any suggestions? – sbala_20 Dec 10 '13 at 22:27
  • defer each call until other completes. If conditions met in callback don't make another call – charlietfl Dec 10 '13 at 23:38

1 Answers1

0

I was able to solve the problem using $broadcast and $on.Here is my solution,

$scope.getwork = function(taskData){
    $scope.workData = workData;
     var getworkQuery = CommunicationsService.getGetworkValue($scope.workData.requestId, $scope.workData.waitTime);
        getworkQuery.query({},function(resp1,$scope){
            $scope.getTaskValue=resp1;
            $rootScope.$broadcast("taskWork_received",resp1);
        });
}

  $scope.$on("taskWork_received", function(scope,taskWorkReceived){
    $scope.getWorkValue=taskWorkReceived;
    switch( $scope.getWorkValue)
    {
        case 'processing':
            getwork($scope.workData );
            break;
        default:
            alert("no response");
            break;
    }
});

Hope this might help some one. Please let me know if there is any problem in this solution. But it works fine for me.

sbala_20
  • 95
  • 2
  • 4
  • 10