0

I have the following:

Node/Express router:

var City = require('./models/city');

    module.exports = function(app) {

        app.get('/cities/:zip', function(req, res) {
            console.log(req.params.zip);
             var query = City.find({"zip" : req.params.zip})
             query.exec(function(err, city) {
                if (err)
                    res.send(err);
                console.log(city);
                res.json(city);
            });
        });

        app.get('*', function(req, res) {
            res.sendfile('./public/views/index.html'); // load our public/index.html file
        });
    };

Angular Service:

angular.module('CityService', []).factory('City', ['$http', function($http) {
    return {
        get : function(zip) {
            var zip = zip.zip
            return $http.get('/cities/' + zip);
        }
     }       
}]);

Angular Controller:

angular.module('CityCtrl', []).controller('CityController', ['$scope', '$http', 'City', function($scope,  $http,  City){
$scope.update = function (zip) {
    $scope.weather = City.get({zip : zip});


    if(zip.length = 5){
        $http.jsonp('http://api.openweathermap.org/data/2.5/weather?zip='+ zip +',us&callback=JSON_CALLBACK').success(function(data){
        $scope.data=data;
        console.log(data.name);
        });
    }
}
}]);

Everything seems to be working fine. However, when I try to log the $scope.weather I get the entire header. I've tried logging $scope.weather.name (I know it's there) and then it returns "undefined". When I check what the server is logging it appears to log the correct JSON response. Any idea of how to get the "name" field of the returned document?

1
  • 1
    City.get({zip : zip}).success(function(response){$scope.weather = response}) Commented Jun 5, 2015 at 1:03

1 Answer 1

1

Replace $scope.weather = City.get({zip : zip}); to City.get({zip : zip}).then(function(response){$scope.weather= response.data});

As we know $http return a promise object so you must have to resolve it. you are not resolving it in service so you have to set $scope.weather in then.

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

16 Comments

With your original "City" implementation, if you use City.get(...).then the parameter is a response object so the you'd need to update the answer to .then(function(response){$scope.weather = response.data}). The .success function spreads the response object out so the first parameter is the data and why PSL's response above works.
When are you doing console.log($scope.weather.city)? Make sure it's after the response has completed (inside then).
@BharatBhushan this is what I get : [ { "_id": "5561204075e68f48dd422a5b", "id": "40553", "county": "Yolo", "state": "CA", "city": "Davis", "longitude": -121.749016, "latitude": 38.521776, "zip": "95616" } ]
Do you only have one weather entry per zip code? If so, you probably want to update your server-side implementation to return a single object instead of an array. If you can have multiple weather entries per zip, then use ng-repeat to go through each one.
its seems your server is responding with array so you should use weather[0].city. or you have to change your response from server to send json object instead of array
|

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.