2013-11-05 70 views
2

我不斷收到錯誤 - TypeError:當我在我的工廠調用方法(database.readOnlyStock())時,不能調用未定義的方法'然後'。也是我覺得有點杞人憂天的是,該功能然後確實運行,因爲我看到console.log消息。越來越TypeError:不能調用方法'然後'未定義在angularjs控制器

這裏是工廠代碼:

 stocks.factory('database', ['$resource', '$http', '$q', function($resource, $http, $q) { 
      var dataMethods = { 
       createConnection: function() { 
        /*moved to onload below */ 
      }, 
       addStock: function(stock, nameofcompany) { 

       var _this = this; 
       console.log("About to add stock"); 

       //Get a transaction 
       //default for OS list is all, default for type is read 
       var transaction = this.db.transaction(["portfolio"],"readwrite"); 
       //Ask for the objectStore 
       var store = transaction.objectStore("portfolio"); 

       //Define a person 
       var stockTemplate = { 
         company_name: nameofcompany, 
         name:stock, 
         created:new Date() 
       } 

       //Perform the add 
       var request = store.add(stockTemplate); 

       request.onerror = function(e) { 
         console.log("failed to add stock to portflio",e.target.error.name); 
         //some type of error handler 
       } 

       request.onsuccess = function(e) { 
         console.log("successfully added stock to portfolio"); 
         //console.log(store); 
         //_this.readOnlyStock(); 

       } 


       }, 
       readOnlyStock: function() { 
        var deferred = $q.defer(); 
       var transaction = this.db.transaction(["portfolio"],"readonly"); 
       var store = transaction.objectStore("portfolio"); 

       // var cursorRequest = store.openCursor(); 

       var arrayOfStocks = []; 




        var keyRange = IDBKeyRange.lowerBound(0); 
        var cursorRequest = store.openCursor(keyRange); 
        cursorRequest.onsuccess = function(e) { 

        var cursor = e.target.result; 
        if(cursor){ 

         arrayOfStocks.push(cursor.value); 
         cursor.continue(); 
        } 
        else{ 


        console.log(arrayOfStocks); 
        console.log('done!'); 

        deferred.resolve(arrayOfStocks); 
        return deferred.promise; 


        //return arrayOfStocks; 
       } 
        } 
       cursorRequest.onerror = function(){ 

        console.log('could not fetch data'); 
       } 


       }, 




       deleteStock: function() {} 
     } 
     //return dataMethods; 
     if (window.indexedDB) { 
      console.log('indexeddb is supported'); 
      var openRequest = indexedDB.open("users_stocks", 4); 
        openRequest.onupgradeneeded = function(e) { 
         var thisDB = e.target.result; 
         if (!thisDB.objectStoreNames.contains("portfolio")) { 
          console.log('created object store'); 
          thisDB.createObjectStore("portfolio", { 
           autoIncrement: true 
          }); 
         } 
        } 
        openRequest.onsuccess = function(e) { 
         console.log('connection opened'); 
         dataMethods.db = e.target.result  
        } 
        openRequest.onerror = function(e) { 
         console.log('could not open connection'); 
        } 
      return dataMethods; 
     } 
     else { 
      console.log('indexedDB not supported'); 

     } 
     }]); 

,然後這裏是我的控制器代碼:

 stocks.controller('portfolio', ['$scope', '$http', 'stockData', 'database', function portfolio($scope, $http, stockData, database) { 


      $scope.getAllStocks = function(){ 
      console.log('running getll stocks'); 

     database.readOnlyStock().then(function(result) { 
      console.log('done'); 

      $scope.allstuff = result; 
      }, function(){ 

      console.log('no'); 
      }); 


     } 


     }]); 

不知道在哪裏的問題。

+1

您沒有從'readOnlyStock'方法返回承諾 –

回答

2

你需要從readOnluStock方法從成功處理程序內它

readOnlyStock: function() { 
     var deferred = $q.defer(); 
     var transaction = this.db.transaction(["portfolio"], "readonly"); 
     var store = transaction.objectStore("portfolio"); 

     // var cursorRequest = store.openCursor(); 

     var arrayOfStocks = []; 




     var keyRange = IDBKeyRange.lowerBound(0); 
     var cursorRequest = store.openCursor(keyRange); 
     cursorRequest.onsuccess = function (e) { 

      var cursor = e.target.result; 
      if (cursor) { 

       arrayOfStocks.push(cursor.value); 
       cursor. 
       continue(); 
      } else { 


       console.log(arrayOfStocks); 
       console.log('done!'); 

       deferred.resolve(arrayOfStocks); 
       //return arrayOfStocks; 
      } 
     } 
     cursorRequest.onerror = function() { 

      console.log('could not fetch data'); 
     } 

     // the return should be here 
     return deferred.promise; 
    }, 
0
if(cursor){ 
    arrayOfStocks.push(cursor.value); 
    cursor.continue(); 
} 

當控制到達該部分返還的承諾,不,它沒有明確地返回任何東西。這意味着默認undefined將由javascript返回。因此,您正試圖撥打thenundefined。您可能需要將其更改爲

if(cursor){ 
    arrayOfStocks.push(cursor.value); 
    cursor.continue(); 
    return deferred.promise; 
} 
相關問題