简体   繁体   中英

TypeError: Cannot call method 'get' of undefined

I am using AngularJs and Protractor for testing api data and trying getting response from the server using restapi and then display that data on console.

Bellow is my Code.

My Service File:

module.exports = {getResponse : function ($http){

    $http.get('http://echo.jsontest.com/title/ipsum/content/blah').
      success(function(data, status, headers, config) {
          console.log(data)
          var jsonData = JSON.stringify(data);
          console.log(jsonData);
          // this callback will be called asynchronously    
          // when the response is available
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    }   

};    

Testcase File :

 var helper = require('./helper');
 describe('A test suite', function() {  

   it('does something to myService', function() {
        var result = helper.getResponse();  
        console.log(result);    
   }); 
  });

Thanks in advance...

Did you include $http in your service delcaration?

angular.module('app').service('YourService', ['$http', function($http) {

// your code

}]);

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