I came across a similar scenario, but my attempts failed because undefined was always returned and kept on getting error after error in the console.
The data is retrieved from the openweathermap API using the angular $resource factory. I understand that i can target and get some of the data i need in the view using ng-repeat but what I also want to break down this data in the controller and access some of the keys and values. When I console.log($scope.weatherResult)
variable that holds the data retrieved from the api I get a Resource object in the console.
How can I go about looping through this object to retrieve the data I need. Its basically objects within objects within arrays. In the view ng-repeat does wonders to get this data but in the controller I don't know how to retrieve it. For example if I console.log($scope.weatherResult.city)
or console.log($scope.weatherResult.list)
the result in the console is always undefined.
Now the data I want to retrieve is within the list array - two days worth of data, but the first object list[0].temp has temperature figures, I want to calculate the sum of those figures and divide it and get the average so I can store it in another variable in the controller and output it in the view.
I'm not sure if im taking a long route here, but i've been in similar situations before but managed to get away with it using the ng-repeat in the view. I need to learn how to do this.
Is it possible to loop through the resource object using angular.forEach()
function and get the data in the temp object? I have attached an image for reference. I am still learning and gaining experience with JavaScript and angular in particular.
$resource('/api/weather/city/Chicago', function(response){$scope.weatherResult = response.data.weatherResult})
The weatherResult from the callback should be accessible. I think you are trying to return it from the Resource object without a callback. i.e.$scope.weatherResult = $resource('/api/weather/city/Chicago'); console.log($scope.weatherResult.city);
. This won't work as is the case with promises. Please do always refer to the docs$resource("http://api.openweathermap.org/data/2.5/forecast/daily", { callback: "JSON_CALLBACK"}, {get: { method: "JSONP"}});