javascript - TypeError: Cannot read property 'success' of undefined - Stack Overflow

I'm creating a service for returning coordinates of a location. But I need some way to return a va

I'm creating a service for returning coordinates of a location. But I need some way to return a value.

angular.module('google.maps', [])
.factory('geocode', ['$http', 'GOOGLE_MAPS_CREDENTIALS', function ($http,GOOGLE_MAPS_CREDENTIALS) {
    return {
        getCoords: function(addr1, zip) {
            $http.get('='
                +encodeURIComponent(addr1+','+zip)+'&sensor=false'
                +'&key='+GOOGLE_MAPS_CREDENTIALS.API_KEY);
        }
    }
}])
.value('GOOGLE_MAPS_CREDENTIALS', { API_KEY: '_____________' });

After this, I have a controller call this function as such:

.controller('AddrNewCtrl', ['$scope','$state','$stateParams','geocode','$rootScope','Address',
    function($scope, $state, $stateParams, geocode, $rootScope, Address) {
    $scope.address = {
        userId: {"__type":"Pointer","className":"_User","objectId":$rootScope.user.id}
    };
    $scope.create = function() {
        geocode.getCoords($scope.address.address1, $scope.address.zipCode)
        .success(function(data) { // <-- this is where the console error is.
            var lat = data.results[0].geometry.location.lat;
            var lng = data.results[0].geometry.location.lng;
            $scope.address.location = new Parse.GeoPoint({latitude:lat,longitude:lng});
        });
        Address.create($scope.address)
        .success(function(data) { $state.go('tabs.address-list'); });
    };
}])

The problem is that when I call success, I get a TypeError:

TypeError: Cannot read property 'success' of undefined

There is little difference between how I implement this service versus any other service and when I console.log the data results in my $http.get() function, I get all the values I'm expecting when I invoke the function in the controller.

I'm looking for one of two answers:

  1. Why is the success callback returning an error?
  2. How can I simply return the a Coordinates object when I invoke my geocode service?

I'm creating a service for returning coordinates of a location. But I need some way to return a value.

angular.module('google.maps', [])
.factory('geocode', ['$http', 'GOOGLE_MAPS_CREDENTIALS', function ($http,GOOGLE_MAPS_CREDENTIALS) {
    return {
        getCoords: function(addr1, zip) {
            $http.get('https://maps.googleapis./maps/api/geocode/json?address='
                +encodeURIComponent(addr1+','+zip)+'&sensor=false'
                +'&key='+GOOGLE_MAPS_CREDENTIALS.API_KEY);
        }
    }
}])
.value('GOOGLE_MAPS_CREDENTIALS', { API_KEY: '_____________' });

After this, I have a controller call this function as such:

.controller('AddrNewCtrl', ['$scope','$state','$stateParams','geocode','$rootScope','Address',
    function($scope, $state, $stateParams, geocode, $rootScope, Address) {
    $scope.address = {
        userId: {"__type":"Pointer","className":"_User","objectId":$rootScope.user.id}
    };
    $scope.create = function() {
        geocode.getCoords($scope.address.address1, $scope.address.zipCode)
        .success(function(data) { // <-- this is where the console error is.
            var lat = data.results[0].geometry.location.lat;
            var lng = data.results[0].geometry.location.lng;
            $scope.address.location = new Parse.GeoPoint({latitude:lat,longitude:lng});
        });
        Address.create($scope.address)
        .success(function(data) { $state.go('tabs.address-list'); });
    };
}])

The problem is that when I call success, I get a TypeError:

TypeError: Cannot read property 'success' of undefined

There is little difference between how I implement this service versus any other service and when I console.log the data results in my $http.get() function, I get all the values I'm expecting when I invoke the function in the controller.

I'm looking for one of two answers:

  1. Why is the success callback returning an error?
  2. How can I simply return the a Coordinates object when I invoke my geocode service?
Share Improve this question asked Dec 9, 2014 at 15:00 Caleb FarukiCaleb Faruki 2,7353 gold badges33 silver badges57 bronze badges 3
  • 2 It's not the "success" callback itself that's the subject of the error. The error means that the getCoords() call is not returning anything. – Pointy Commented Dec 9, 2014 at 15:04
  • 1 you are not returning a promise on getCoords, you should have return $http.get – Avraam Mavridis Commented Dec 9, 2014 at 15:06
  • Wow, thank you. That resolved it @Pointy , feel free to put that in an answer and I'll give you creds for it. – Caleb Faruki Commented Dec 9, 2014 at 15:07
Add a ment  | 

1 Answer 1

Reset to default 4

You should have

 getCoords: function(addr1, zip) {
         return $http.get('https://maps.googleapis./maps/api/geocode/json?address='
            +encodeURIComponent(addr1+','+zip)+'&sensor=false'
            +'&key='+GOOGLE_MAPS_CREDENTIALS.API_KEY);
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744871233a4598273.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信