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:
- Why is the
success
callback returning an error? - 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:
- Why is the
success
callback returning an error? - How can I simply return the a Coordinates object when I invoke my
geocode
service?
-
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
1 Answer
Reset to default 4You 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条)