I am currently working on a service that retrieves coordinates for a specific location. However, I am encountering an issue when trying 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.com/maps/api/geocode/json?address='
+encodeURIComponent(addr1+','+zip)+'&sensor=false'
+'&key='+GOOGLE_MAPS_CREDENTIALS.API_KEY);
}
}
}])
.value('GOOGLE_MAPS_CREDENTIALS', { API_KEY: '_____________' });
Following this, I have a controller that calls the function in this manner:
.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 issue arises when I try to call success, as it results in a TypeError:
TypeError: Cannot read property 'success' of undefined
Despite following similar implementation patterns with other services, I encounter this problem. Upon logging the data results from my $http.get()
function, I see the expected values when invoking the function in the controller.
I am seeking one of two resolutions:
- Why is the
success
callback causing an error? - How can I efficiently retrieve the Coordinates object when using my
geocode
service?