Error: The property 'success' is not defined and cannot be read

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:

  1. Why is the success callback causing an error?
  2. How can I efficiently retrieve the Coordinates object when using my geocode service?

Answer №1

Make sure to

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to launch a browser in full screen mode using a command line interface?

Is there a clever way to launch a web browser in full screen mode from the command line? The browser's full screen API only activates in response to user interaction. I simply want to show data on a large monitor without any unnecessary elements lik ...

Checkbox selection problem caused by repeated clicking

What I accomplished 1. Generated an array of checkboxes with unique numeric ID 2. Utilized jQuery to toggle checkbox status on click of <td>(also with unique numeric ID) 3. Implemented check-uncheck logic using jQuery. Issue : Initially, ...

Issue with Vue multiselect preventing creation of new tags

Having a functional Vue multiselect feature that utilizes an autocomplete function via an axios call to a database. The process of retrieving DB results, adding them as options, and turning them into tags works seamlessly. The main issue arises when tryin ...

Angular multiple checkbox array iteration in ng-repeat

I am currently working on a form using Angular and facing a challenge with a specific section. Within the form, there is a 'Divisions' segment that includes a list of checkboxes generated dynamically from an API call. Users are required to selec ...

Understanding Arrays in Angular JSIn this article, we

I am new to working with Angular JS. Currently, I am populating an array and attempting to showcase its contents on the HTML page using ng-repeat. $scope.groupedMedia = []; // Elements are being added through a for loop. $scope.groupedMedia[year].push(r ...

Is there a way to transform a six-digit input into a date format using vue/JavaScript?

I've explored various solutions for this issue, but they all seem to be backend-focused. What I need is a Vue/JavaScript method. I have a string containing IDs, with the first 6 digits representing the date of birth and the final 4 digits being a pers ...

Refreshing the identification of a button

I have been working on updating an ID with a value from another button. Here is my current progress: $('.viewemployment').on('click', function(e){ var url = '<?php echo Config::get('URL'); ?>dashboard/employmen ...

Stop the duplication of the HTML content to ensure only one copy is saved

Recently, I have been coding a feature that saves the HTML of the currently displayed page to another file upon pressing a button. However, I encountered an issue where if the button is pressed multiple times quickly, the saved file contains incorrect HTML ...

React-router causing issues with Redux integration

Currently, I'm utilizing the following libraries: react v16.2.0, react-redux v5.0.7, react-router-dom v4.2.2, redux v3.7.2 The main goal is to update some properties within the Auth component and have these changes reflected when the user visits the ...

What is the best method to make an email address distinct?

<body> <p>Please input your email address:</p> <input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email" placeholder="Email Address"> <input onclick= "validateEmail(email)" type="su ...

I'm looking for an easy way to generate a special effect when my mouse interacts with a div using HTML, CSS, or JavaScript

I'm attempting to replicate this interesting effect where a div is surrounded by a container when the mouse hovers over it. It looks pretty cool, like in this image here: https://i.stack.imgur.com/p0epq.png Does anyone have any suggestions on how I ...

Navigating to a new page by clicking a button

I am trying to redirect to a different web page when a button is clicked. Below is the code snippet I am working with: JavaScript code snippet: app.controller('myCtrl', ['$scope', '$location', function($scope, $location) { ...

What is the best way to continuously swap out images inside a div every second, except when the mouse hovers over

I recently posted a question on Stack Overflow about displaying an image in a div when a specific link is hovered over, and thanks to the help of user adeneo, I was able to find a solution (jsFiddle): HTML Markup: <div id="imgs"> <img src="h ...

What is the process for changing to a different page in NativeScript?

Having trouble creating a simple button in NativeScript labeled 'login'? I've created a login component, but when testing the code, it shows an error stating "no known component for element Login." <template> <Page> <TabV ...

Attempting to set up an Ajax webform with various outputs, but encountering issues with functionality

I'm encountering an issue while creating interactive exercises. I want to display correct or incorrect answers immediately after submission using JSON for retrieving responses, as suggested in a forum. However, my AJAX code isn't working at all. ...

Utilizing setInterval() for straightforward and continuous polling operations

When developing a basic web application that requires periodic data updates for the user, is there any disadvantage to using setInterval() to fetch JSON from an endpoint instead of utilizing a more formal polling framework? As an illustration, let's ...

Why isn't the DIV I created in JavaScript appearing when the page loads?

I've been struggling to get the DIV generated by the Javascript code, particularly in the function parameter id within creatediv('xdiv', html, 100, 100, 10, 10) where id = 'xdiv'. <!DOCTYPE html> <html> <head> &l ...

Incorporate parameters and data attributes with ScriptInjector in GWT

Is there a way to pass the id=someScript and param1=value1 to ScriptInjector in GWT? For example, consider the following JS code: <script src="https://blah.com/someScript.js" id="someScript" param1="value1"></script> In GWT, you can load it l ...

Button placed within a jade table cell

I'm struggling to make a button appear in each row of the table. I am new to working with Jade and Node.js, so I can't seem to figure out why it's not showing up. Here is my Jade file: html head body table.table.table(border='1 ...

Illuminate specific areas of a mapped image upon hovering over text on a webpage

Scenario: There is an image on the page with various mapped areas. A list of text elements is also present on the page. Desired functionality: Hovering over different text items in the list should result in highlighting corresponding areas on the mappe ...