0

I have seen this question many times but I can't figure out why one of my controller functions (controller 1) works fine and one (controller 2) doesn't.

Controller 1:

angular.module('app').controller('MyCtrl1',function($scope,MyFactory)) {

  //This function works great
  MyFactory.deleteItem(item.id).then(function(response) {
    //woot woot I am here and I am fine
  });
}); //end controller 1

Controller 2:

angular.module('app').controller('MyCtrl2',function($scope,MyFactory)) {

 //Function #2 that doesn't work ... 'then' is undefined
  MyFactory.createItem(item).then(function(response) {
    //booo hooo I never get here and I am definitely not fine
  });
}); //end controller 2

The factory:

.factory("MyFactory", function($http) {

var service = [];

service.deleteItem = function(itemId) {
  return $http({
    method: "delete",
    url: "http://www.example.com",
    params: //valid params go here
  }).then(function(response) {
    console.log("DELETING!!!!");
    return response.data;
  });
}

service.createItem = function(post) {
  return $http({
      url: '?create',
      method: 'post',
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      data: payload //an object
    }).then(function(response){      
      console.log(response.data); //we are fine here. there is a valid response 
      return response.data; 
   });
}
return service;
}); //end factory

The error thrown when executing 'createItem' is 'Cannot read property 'then' of undefined' What am I missing ?

4
  • 1
    use $promise.then(function() { ... it will definately work Commented Feb 28, 2016 at 18:58
  • also u can use success and error function Commented Feb 28, 2016 at 19:00
  • I did try success and error..no difference. Commented Feb 28, 2016 at 19:02
  • This also did not work: var promise = MyFactory.createItem(item); promise.then(function(response) { //booo this didnt work either }); Commented Feb 28, 2016 at 19:03

2 Answers 2

3

You are missing the return in the createItem statement:

service.createItem = function(post) {
   return $http({
      url: '?create',
      method: 'post',
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      data: payload //an object
    }).then(function(response){      
      console.log(response.data); //we are fine here. there is a valid response 
      return response.data; 
   });
}

Without it, there is no return value (which is undefined) on which you can't chain the .then.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you...that was true. But still getting undefined ??
@lilbiscuit Hehe.. happens to all.
OK I removed the console.log and the function appears to work. But why ?
The console.log has nothing to do with it. You needed a return of the $http, that's what was missing. all console.log does it print to your dev console.
-2

If you are not returning $http the use like this :

$http({
  url: '?create',
  method: 'post',
  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  data: payload //an object
}).$promise.then(function(response){      
  console.log(response.data); 
  return response.data; 
});

2 Comments

This is not a negative part of answer , it will also work
$promise is not a property returned by the $http service. You are confusing $resource objects with $http promises.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.