This is module
(function(){
/**
* app Module
*
* Description
*/
angular.module('app',[]);
})();
I have this service
(function(){
'use strict';
angular
.module('app')
.factory('homeservice',homeservice);
homeservice.$inject = ['$http']
function homeservice($http){
var service = {
test : test
}
return service;
/**
* get articles
* @return {[type]} [description]
*/
function test(){
$http.get('/test')
.then(testSuccess)
.catch(testError)
function testSuccess(response){
return response.data;
//console.log(response.data) this line prints data to console
}
function testError(error){
return error;
}
}
}
})();
This is my controller which calls function
(function(){
angular.module('app')
.controller('HomeController',HomeController);
HomeController.$inject = ['homeservice']
function HomeController(homeservice) {
var vm = this;
test();
function test(){
homeservice.test().then(function(response){
console.log(response);
});
}
}
})();
I keep getting the error - Cannot read property 'then' of undefined when I call a method in my controller. I have used this way before and It works properly.
ANSWER
function test(){
return $http.get('/test') // return was missing here
.then(testSuccess)
.catch(testError)
function testSuccess(response){
return response.data;
//console.log(response.data) this line prints data to console
}
function testError(error){
return error;
}
}
I was missing return at $http.get('/test')