最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS returning a promise causing 'then' of undefined - Stack Overflow

programmeradmin1浏览0评论

I have the following code as part of my AngularJS app:

myApp.factory('NewContactData', function($http,$log, $q) {
 return {    
   saveContact: function(contact){
     var deferred = $q.defer();  
     ...
     ...
     return deferred.promise;     
   }, 
   getContacts: function(){
     var gcdeferred = $q.defer();  
     ...
     ...
     return gcdeferred.promise;         
   }      
 }; 
});      

When I try to call getContacts from my app controller as follows:

myApp.controller('ContactsController',
        function ContactsController($scope, $location, NewContactData){


    NewContactData.getContacts().$promise.then(
            function(response){
               console.log(response);
            },
            function(status){
               console.log(status);
            }
    );         

I get error:

TypeError: Cannot call method 'then' of undefined
 at new ContactsController

Even though as it is shown from my code that getContacts return promise. Can someone please tell me what I am missing here? Thanks

I have the following code as part of my AngularJS app:

myApp.factory('NewContactData', function($http,$log, $q) {
 return {    
   saveContact: function(contact){
     var deferred = $q.defer();  
     ...
     ...
     return deferred.promise;     
   }, 
   getContacts: function(){
     var gcdeferred = $q.defer();  
     ...
     ...
     return gcdeferred.promise;         
   }      
 }; 
});      

When I try to call getContacts from my app controller as follows:

myApp.controller('ContactsController',
        function ContactsController($scope, $location, NewContactData){


    NewContactData.getContacts().$promise.then(
            function(response){
               console.log(response);
            },
            function(status){
               console.log(status);
            }
    );         

I get error:

TypeError: Cannot call method 'then' of undefined
 at new ContactsController

Even though as it is shown from my code that getContacts return promise. Can someone please tell me what I am missing here? Thanks

Share Improve this question asked Mar 12, 2014 at 15:37 MChanMChan 7,21227 gold badges85 silver badges134 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

it's just NewContactData.getContacts().then( since you are already returning a promise

Try this:

NewContactData.getContacts().then(
        function(response){
           console.log(response);
        },
        function(status){
           console.log(status);
        }
);  
发布评论

评论列表(0)

  1. 暂无评论