3

I am using Angular.js controller named 'forecastController'.

Angular.js Controller code is:

weatherApp.controller('forecastController', 
    ['$scope','$routeParams','cityService', 'weatherService', function($scope, $routeParams , cityService, weatherService)
{    
    $scope.city=cityService.city;
    $scope.days=$routeParams.days || '2' ;
    $scope.weatherResult=weatherService.GetWeather($scope.city, $scope.days); //I get valid json response, the format is mentioned down below.
    console.log($scope.weatherResult.city.name); //Not working, this is the problem
}]);

Json object '$scope.weatherResult' is:

{
  "city":     
  {
    "id": 2643743,
    "name": "London",
    "coord": {
      "lon": -0.12574,
      "lat": 51.50853
    },
    "country": "GB",
    "population": 0
  },
  "cod": "200"
}

My service is

weatherApp.service('weatherService', ['$resource',function($resource){
    this.GetWeather=function(city, days){

        var weatherAPI=$resource("http://api.openweathermap.org/data/2.5/forecast/daily?APPID={{MY_API_KEY_GOES_HERE}}",{
        callback:"JSON_CALLBACK"}, {get:{method:"JSONP"}});

        return weatherAPI.get({q:city, cnt:days});     
    };
}]);

My 'forecastController' receives valid $scope.weatherResult. And in the HTML view I can access weatherResult json object properties. I have ensured it properly. For an example, {{weatherResult.city.name}} works. But, if I try to print in console.log inside my 'forecaseController' I get value undefined. The json data I am getting is from http://openweathermap.org/api

The error I get is:

TypeError: Cannot read property 'name' of undefined
    at new <anonymous> (http://127.0.0.1:50926/controllers.js:19:42)
    at e (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:36:215)
    at Object.instantiate (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:36:344)
    at https://code.angularjs.org/1.3.0-rc.2/angular.min.js:72:460
    at link (https://code.angularjs.org/1.3.0-rc.2/angular-route.min.js:7:268)
    at Fc (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:68:47)
    at K (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:57:259)
    at g (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:49:491)
    at https://code.angularjs.org/1.3.0-rc.2/angular.min.js:49:99
    at https://code.angularjs.org/1.3.0-rc.2/angular.min.js:50:474 <div ng-view="" class="ng-scope">

1 Answer 1

1

Your service weatherService.GetWeather likely returns a promise. Until that promise is resolved, the properties in your $scope.weatherResult object will be undefined. Use the promise then method:

$scope.weatherResult = weatherService.GetWeather($scope.city, $scope.days);
$scope.weatherResult.$promise.then(function() {
    console.log($scope.weatherResult.city.name);
});

Update: As per my comment, the service is returning a $resource object. This object has a $promise property.

Link To Codepen

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

4 Comments

Hi Corey, I tried your suggestion, It is not working. I have added the service code if you can have a look and suggest me something else, too. Thanks for your help.
It's returning a $resource object which has a $promise property. I've updated the answer to include the $promise.then method. It's untested...
I tried as per your 2nd solution. Still not working. Same error.
Noticed my mistake and broke it out into two lines. Also, added a link with a working example.

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.