Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with AngularJS is that, i cannot assign a value to an outer variable in the $http.get method. Here is my code snippet:

.factory('MediaService',['$http', function($http, $q){
    return {
        getGalleryIds : function() {
            galeriURL = 'http://api.tika.gov.tr/www/tr/galleries?';
            return $http.get(galeriURL, { cache: true}).then(function(response){
                contentIdList = response.data.data;
                var contentList = [];
                for (i=0;i<contentIdList.length;i++) {
                    var galeriContentURL = 'http://api.tika.gov.tr/www/tr/galleries/' + contentIdList[i].id;
                    contentList[i] = $http.get(galeriContentURL, { cache: true})
                        .then(function(response){
                            return response.data;
                        });
                }
                console.log(contentList);
                return contentList;
            });
        }
    }
}]);

My problem is at console.log(contentList); line I'm getting Promise Array because of I cannot assign a value to outer variable.

[Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise]

How can I assign value to var contentList = [ ]; variable in for loop $$http.get and at line console.log(contentList); get Object Array as follows

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
share|improve this question

1 Answer 1

Problem lies within this part:

contentList[i] = $http.get(galeriContentURL, { cache: true})
                        .then(function(response){
                            return response.data;
                        });

$http returns a promise which you are storing in contentList array, and not the data., when you use a return inside the success function, the data will not be returned to content list.


You need to replace that code with this:

 $http.get(galeriContentURL, { cache: true})
                        .then(function(response){
                            contentList[i] = response.data;
                        });

This does not eally guarantee that all the data objects stored in array will be in the same order as the order in which they are requested.

share|improve this answer
    
Thanks for answer but this is not working for me, because at line console.log(contentList); I'm getting a null array. Before return at console i'm reading [ ] not [Object,Object]. –  ozhanli Feb 17 at 9:41
    
@ozhanli:: yes then there is some other problem when you return the data from that url. can you try console.log(response) inside your then() function –  NoDownvotesPlz Feb 17 at 9:43
    
no there is no problem with data from url. when i use console.log as http.get().then(){console.log} i'm getting object. –  ozhanli Feb 17 at 9:50
    
Also if I populate contentList as in question I'm getting promise but I need Object instead Promise –  ozhanli Feb 17 at 9:52
    
@ozhanli promise will be always returned by $http: try this contentList.push(response);, instead of contentList[i] = response.data; in my answer, should work :) –  NoDownvotesPlz Feb 17 at 9:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.