2

我不断收到错误-TypeError:当我在工厂中调用方法(database.readOnlyStock())时,无法调用未定义的方法'then'。我还发现有点 ackward 是因为我看到了 console.log 消息,所以该函数会相应地运行。

这是工厂代码:

        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');
          });


        }


        }]);

不确定问题出在哪里。

4

2 回答 2

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;
    },
于 2013-11-05T03:41:54.100 回答
0
if(cursor){
  arrayOfStocks.push(cursor.value);
  cursor.continue();
}

当控件到达这部分时,它不会显式返回任何内容。这意味着默认情况下undefined将由javascript返回。因此,您正在尝试调用then. undefined您可能希望将其更改为

if(cursor){
  arrayOfStocks.push(cursor.value);
  cursor.continue();
  return deferred.promise;
}
于 2013-11-05T03:42:01.757 回答