0

I am using this code:

$http.get('/api/users').
success(function(data) {
    $scope.authors = data;
}).
error(function() {
    console.log('API error - config.')
}); 

And somewhere below (much bellow):

for (var i = 0; i < $scope.authors.length; i++) {
    ...
};

Sometimes it happens that $scope.authors are not avaliable yet. Is there any way to solve this?

UPDATE

This is the whole block structure:

// author

$http.get('/api/users').
success(function(data) {
    $scope.authors = data;
    processAuthors();
}).
error(function() {
    console.log('API error - config.')
});    

// if updating form

$scope.$on('$routeChangeSuccess', function() {
    if($routeParams.id) {

        $http.get('/api/offers/' + $routeParams.id).
        success(function(data) {

            // author

            function processAuthors() {
                for (var i = 0; i < $scope.authors.length; i++) {
                    if($scope.authors[i].email == data.author.email) {
                        $scope.author = $scope.authors[i];
                    };
                };                    
            }
1
  • 1
    Make sure not to call the function containing the for loop until the authors are available. $http is asynchronous. You can't expect the response to be there immediately. For more help, post a complete example. Commented Jul 28, 2015 at 11:08

2 Answers 2

1

Put the loop in the succes part:

$http.get('/api/users').
success(function(data) {
    $scope.authors = data;
    for (var i = 0; i < $scope.authors.length; i++) {
       ...
   };
}).
error(function() {
    console.log('API error - config.')
});
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, it doesn't matter how much below it is - you still need to call it from inside the callback because it's an async call:

function processAuthors() {
    for (var i = 0; i < $scope.authors.length; i++) {
      ...
    };
}

And then:

$http.get('/api/users').
success(function(data) {
    $scope.authors = data;
    processAuthors();
})

I used a function to make it look cleaner, but you can copy your code that depends on the callback inside it.


UPDATE

function processAuthors(data) {
            for (var i = 0; i < $scope.authors.length; i++) {
                if($scope.authors[i].email == data.author.email) {
                    $scope.author = $scope.authors[i];
                };
            };                    
        }

$scope.$on('$routeChangeSuccess', function() {
    if($routeParams.id) {

        $http.get('/api/offers/' + $routeParams.id).
        success(function(data) {

            // author
            processAuthors(data);  // just call it here, define it outside

13 Comments

This solution is actually great because my code is really much bellow :)
Yeah, organizing code block into functions is always good. But sometimes you just need to do a little hack and make it messy. :)
Indeed. Although my function processAuthors is inside $scope.on and $http.get and I am getting ReferenceError: processAuthors is not defined. Any ideas?
You can't have it inside another callback because of JS scopes - it won't be available outside that function. If you show me that whole block, I'll probably be able to fix it.
That block is really long. Where can I put it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.