Currently, I am working on a web application and facing an issue in parsing an object. I attempted to retrieve the IDs of all results but unfortunately, it is returning undefined in my console.
Here's what I tried:
var app = angular.module("DB", []);
app.controller("Controller", function($scope, $http) {
$http.defaults.headers.common["Accept"] = "application/json";
$http.get('api_url').
success(function(data, status, headers, config) {
$scope.games = data.results;
$scope.id = data.results.id;
//Do i need a foreach here because it doesn't loop through all records and it gives me undefined.
$http.get('http:/api/id/' + $scope.id + '?api_key=', function(e){
}).
success(function(data, status, headers, config) {
$scope.details = data;
console.log(data);
//this returns the complete JSON
});
}).
error(function(data, status, headers, config) {
//handle errors
});
});
The initial http.get request retrieves a JSON with the following structure:
"results": [
{
"easy": false,
"id": 1,
"title": "title",
},
{
"easy": false,
"id": 2,
"title": "title",
},
{
"easy": true,
"id": 2,
"title": "title",
}
]
The goal of the second http.get request is to extract all the IDs from the JSON and initiate a new GET request:
$http.get('http:/api/id/' + $scope.id + '?api_key=', function(e){
}).
success(function(data, status, headers, config) {
$scope.details = data;
console.log(data.data);
});
})
The issue lies in $scope.id = data.results.id;
as it returns nothing. Should I consider using a foreach loop or another method to iterate through all the data?
I also attempted to display the information using:
<div ng-repeat="detail in details">
{{ detail }}
{{ detail.adult }}
</div>
However, nothing gets displayed (note that I changed $scope.id = data.results.id; to $scope.id = data.results[0].id; for testing purposes).
The JSON structure for the second GET request is as follows:
{
"adult": false,
"collection": {
"id": 131295,
"name": "Collection",
},
"levels": [{
"id": 28
}, {
"id": 12
}, {
"id": 878
}],
"homepage": "google",
"id": 100402,
"overview": "lorem ipsum"
}
I am unable to access the object using {{ detail.adult }} for example.