0

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.

Angular Resource loop

4
  • In the controller, if $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 Commented Oct 27, 2016 at 3:47
  • You want to get the data in the temp object using forEach or do you want to add the temperatures in temp using forEach? Commented Oct 27, 2016 at 3:54
  • @ThatBird retrieve the data from the temp object and have it pushed into a variable like so $scope.temperature = [13.47, 12.33, 13.47, 10.04, 10.04, 10.07]. Commented Oct 27, 2016 at 4:00
  • @Manesh the callback is there, here is a snippet. $resource("http://api.openweathermap.org/data/2.5/forecast/daily", { callback: "JSON_CALLBACK"}, {get: { method: "JSONP"}}); Commented Oct 27, 2016 at 4:11

1 Answer 1

0
$scope.temp = []
angular.forEach(list[0].temp, function(value,key){
  $scope.temp.push(value);
})

See if this helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.