Marek Marek - 3 years ago 141
AngularJS Question

Why function not returning promise 'Cannot read property 'then' of undefined'?

Is coming up this error 'Cannot read property 'then' of undefined' i wonder why factory not returning object.

Controller:

angular
.module('sfcMachinesFieldDefinitions')
.controller('MachinesListController', ['$translate', '$http', '$scope', '$state', 'ListOfMachines',
function ($translate, $http, $scope, $state, ListOfMachines) {
var vm = this;
vm.allMachines = function () {
return ListOfMachines.allMachines().then(function (response) {

console.log('The respounse' + JSON.stringify(response))
vm.response = response;

})
return response;
};


Service Factory:

angular
.module('sfcMachinesFieldDefinitions')
.factory('ListOfMachines', ['$http', function ($http) {
return {

allMachines: function () {
$http.post('MachineList/GetAllMachines/').then(function (response) {

return response;
})}
};
}]);

Answer Source

It doesn't return an object because you're not returning it.

It should be

...
allMachines: function () { 
    return $http.post('MachineList/GetAllMachines/')...
}
...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download