For a considerable amount of time, I have been facing some challenges with an API call. Even though the console.log in my service is returning the necessary data, the controller seems to receive an empty object. I suspect there might be an issue with how I am handling my promises, but despite thorough inspection, I cannot pinpoint the problem. Currently, I am utilizing $q.all() to concatenate multiple get requests.
Below is the code snippet for my controller function:
$scope.getData = function(input) {
exampleService.getData(input)
.then(function(data) {
console.log(data);
$scope.savedData = data;
}, function(error) {
alert(error); // displays an alert message with -1 upon encountering an error
});
};
And here is the service function implementation:
this.getData = function(input) {
$q.all([
$http.get(url1),
$http.get(url2)
]).then(function(response) {
console.log(response); // returns an array of objects that are required
return response;
});
return $q.all();
};
While attempting to return $q.all().promises, an error appeared in the console stating:
cannot read property 'then' of undefined
Ultimately, my goal is to access a specific property within each nested object in the array. For instance:
var returnedPromise = [
{dataIDontNeed: 1, {dataIwant}},
{dataIDontNeed: 2, {dataIwant}},
{dataIDontNeed: 3, {dataIwant}}
]
I am seeking guidance on retrieving the dataIwant object from the array of objects before sending back the promise. Any assistance provided would be greatly appreciated.