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"}