I am currently working with a piece of source code that looks like this:
var projectPromises = $http.get('http://myapi.com/api/v3/projects?private_token=abcde123456');
$q.all([projectPromises]).then(function(data) {
console.log(data);
return data[0];
}).then(function(projects) {
var data = projects.data;
var promises = [];
var file = [];
for(var i = 0; i < data.length; i++){
var url = 'http://myapi.com/api/v3/projects/' + data[i].id + "/owner";
promises.push($http.get(url));
}
console.log(promises);
$q.all(promises).then(function(user) {
console.log(user);
}, function(error) {
console.log("error here");
console.log(error);
});
Allow me to elaborate on the intention behind my source code.
To begin, the initial API call retrieves a list of projects which I store in projectPromises. After obtaining the project list, each project contains an ID. Subsequently, I iterate over the projects and send corresponding HTTP requests to retrieve the owner of each project.
Following that, I utilize Angular's q module to defer the promises array and display it in the console using:
console.log(user);
In this section, nothing is being logged. Upon further investigation, I discovered that certain projects do not have a user list. In such cases, a 404 error occurs, while a 200 status code is returned otherwise. As a result, the promises array contains objects from both scenarios, causing errors when deferring the promises using Angular q. Unfortunately, I am unsure how to address this issue.
The ultimate goal of this script is to retrieve the owners of each project and store them in an array.