简体   繁体   中英

Angularjs TypeError: Cannot read property 'data' of undefined

I'm using an angular directive (angucomplete-alt) to generate a list of suggestions as I type into ta field. I have an issue that if I make the request using $http like below the directive picks up the data and displays it.

 return $http.get(
            myurl,
            {
                params: {
                    code: strQuery.toUpperCase()
                }
            }
        );

Alertnatively, if I return the data in a promise the directive just keep giving me an error. How can I get to this work properly as it seems like even though I'm calling the same endpoint I get way different responses

 return service.mymethod(strQuery)
            .then(function(data) {

               console.log(data);

        });

Although it is unclear what you are trying to achieve this is one way to call a service:

var app = angular.module('myApp', []);
app.controller('myController', function($http) {
  $http.get(myUrl, { params: {  code: strQuery.toUpperCase() }})
  .then(
    function (response, status) { // on success
      console.log(response);
    }, 
    function(error, status){ // on error
      console.log(error);
  });
}); 

$http.get returns a promise which is a pattern for handling asynchronous operations. A promise runs asynchronously and return values (on success or on error) when they are done processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM