1

I have the following function where I push all promises in an array and then using a then function, I mean to travserse though the promises and get the data arrays that each promise has (the service returns an array of jsons)

     var getPP= function(){
                        var promises = [];
                        angular.forEach($scope.myObjects, function(myObject) {

                            var urlPath = <someurl>+myObject.name;
                            var promise = $http({
                                url   : urlPath,
                                method: 'GET'
                            });
                            promises.push(promise);
                        });

                        $q.all(promises).then(function(data){
                            for(j=0; j < data.length; j++){
                                var jsonArray= [];
                                console.log('response = '+data[j]+' and j = '+j);--> 

**this log statement just prints a [object object] 
        even though the json array is pretty huge as returned by the service. What are these two objects?**

                                for (k = 0; k < data[j].length; k++) { 

    **--> It never gets here and does not logs anything below**
                                   console.log('inside loop'); 
                                   console.log(data[j][k].id+data[j][k].empname);
                                }
                           }
                       });

Am I retrieving data correctly? Any inputs how we can do this? Please read the two comments in the code for the issues I am facing.

Thanks!!

EDIT 1 - The json looks something like this -

[{
    "id": 1,
    "empname": "emp1"
},
{
    "id": 2,
    "empname": "emp2"
},
{
    "id": 3,
    "empname": "emp3"
}]

EDIT 2: I also tried changing my promise variable definition as below, but same issue

 var promise = $http({
                    url   : urlPath,
                    method: 'GET'
                }).success(function(data, status){
                    defer.resolve(data);
                })
                    .error(function(data, status) {
                        defer.reject(status);
                    });

EDIT 3: As per your suggestion and using the EDIT 2 promise variable format --following is the output in the console when I use the following two console statements:

console.log('response = '+JSON.stringify(data[j])+' and j = '+j);

Console output:

response = {"data":[{"id": 1,"empname": "emp1"},{"id": 2,"empname": "emp2"},{"id": 3,"empname": "emp3"}],"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://localhost:8091/api/types?isoCode=USA&groupName=AdvisorySpeedLimit2_40","headers":{"Accept":"application/json, text/plain, */*","X-Requested-With":"XMLHttpRequest"},"requestTimestamp":1458169519915,"responseTimestamp":1458169520190},"statusText":"OK"}
Tisha
  • 827
  • 3
  • 11
  • 34
  • Apart from the missing `var`s on `j` and `k` this looks fine. Show us how the JSON that `$http` receives for each name looks like and we can tell you [how to correctly traverse it](http://stackoverflow.com/q/11922383/1048572). – Bergi Mar 16 '16 at 22:45
  • Added edit 1 for the json – Tisha Mar 16 '16 at 22:51
  • You probably want to use `console.log(data[j][k].id+data[j][k].empname);` – Bergi Mar 16 '16 at 22:55
  • Sorry, forgot to write that here. That's how I am using it but its not working. – Tisha Mar 16 '16 at 22:58
  • Try `console.log('response = '+JSON.stringify(data[j])+' and j = '+j);` or `console.log('response', j, data[j]);` to see the actual results in the console instead of "[object Object]". – Bergi Mar 16 '16 at 23:02
  • Please see my edited description Bergi – Tisha Mar 16 '16 at 23:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106518/discussion-between-tisha-and-bergi). – Tisha Mar 16 '16 at 23:16
  • 2
    data[j].data[k] did the trick!! :) – Tisha Mar 16 '16 at 23:18

1 Answers1

0

Thanks for all your suggestions @Bergi!

data[j].data[k] did the trick and shows the right data.

mido
  • 24,198
  • 15
  • 92
  • 117
Tisha
  • 827
  • 3
  • 11
  • 34
  • Please don't add "thank you" as an answer. Instead, **[accept the answer](http://stackoverflow.com/help/accepted-answer)** that you found most helpful. - [From Review](/review/low-quality-posts/11660052) – Nic Mar 17 '16 at 01:23
  • @QPaysTaxes: They're thanking for my comments, not for an answer. Self-answering (and accepting that once its eligible) is fine. – Bergi Mar 17 '16 at 01:25
  • @Bergi The proper response is to write an answer, then. Self-answering is fine, but "this thing worked for me" isn't a good answer; "this thing worked and here's why" is, and you can write that. – Nic Mar 17 '16 at 01:26
  • Well I can delete my answer and let Bergi write one. Basically, he helped me identify what exactly the returned objects are, which helped me to access them properly. – Tisha Mar 17 '16 at 03:39