【问题标题】:angularJS/ionic cannot read property 'then' of undefinedangularJS/ionic 无法读取未定义的属性“then”
【发布时间】:2015-04-25 12:08:11
【问题描述】:

我正在将缓存添加到我的应用程序中,从缓存数据、使其过期和重新缓存一切正常。当我实现在没有连接的情况下再次将相同过期数据放入缓存的功能时,我收到此错误:Cannot read property 'then' of undefined

这是我的控制器的代码:

.controller('ItemsCtrl', function($scope, $http, DSCacheFactory) {

    //handle the catche to hold the items lits.
    self.itemsCache = DSCacheFactory.get("itemsCache");
    /*
    *Function to re-refresh the cach after expire
    *re-use the old cache if no internet connection available
    */
    self.itemsCache.setOptions({
        onExpire: function(key, value){
            getItems()
                .then(function(){
                console.log("Items Cache was automatically refreshed", new Date());
            }, function(){
                console.log("Errorgetting data put expired item back in the cache", new Date());
            });
        }
    });

    function getItems(){
        var cacheKey = "items",
            itemsData = self.itemsCache.get(cacheKey);
        if(itemsData){
            // if data in the cache dont make  HTTP calls.
            console.log("Found Data in cache", itemsData);
            //recive Data from the cache
            $scope.items = itemsData;
        }
        else{//if no data in the catch make HTTP calls.
            //HTTP request to get the items data.
            $http.get('services/data.json')
             .success(function(data){
                console.log("Recived data via HTTP");
                //caching the data recived from the http calls.
                self.itemsCache.put(cacheKey, data);
                $scope.items = data;
            });
        }//end of the else statment
    }
    getItems();
})

【问题讨论】:

    标签: angularjs ionic-framework caching


    【解决方案1】:

    函数 getItems 没有返回任何东西 - 看起来你打算返回一个承诺,所以它应该如下所示。

    当您在缓存中找到该项目时,您可以使用 $q.when 创建一个在参数不是 Promise 时已经解析的 Promise。

    当你没有找到它时,你可以从 $http.get 中返回链式承诺。

    .controller('ItemsCtrl', function($scope, $http, DSCacheFactory, $q) {
    
        //handle the cache to hold the items list.
        self.itemsCache = DSCacheFactory.get("itemsCache");
        /*
        *Function to re-refresh the cache after expire
        *re-use the old cache if no internet connection available
        */
        self.itemsCache.setOptions({
            onExpire: function(key, value){
                getItems()
                    .then(function(itemsData){
                    console.log("Items Cache was automatically refreshed", new Date());
                }, function(){
                    console.log("Error getting data put expired item back in the cache", new Date());
                });
            }
        });
    
        function getItems(){
            var cacheKey = "items",
                itemsData = self.itemsCache.get(cacheKey);
            if(itemsData){
                // if data in the cache dont make  HTTP calls.
                console.log("Found Data in cache", itemsData);
                //receive Data from the cache
                $scope.items = itemsData;
                return $q.when(itemsData);
            }
            else{//if no data in the catch make HTTP calls.
                //HTTP request to get the items data.
                return $http.get('services/data.json')
                 .success(function(data){
                    console.log("Received data via HTTP");
                    //caching the data received from the http calls.
                    self.itemsCache.put(cacheKey, data);
                    $scope.items = data;
                    return data;
                });
            }//end of the else statement
        }
        getItems();
    })
    

    【讨论】:

    • 我试过你的解决方案它没有用,我也尝试以不同的方式应用这个概念,仍然没有工作仍然无限的 HTTP 调用和 undifined ill post my soultion 的相同错误没有作为答案
    【解决方案2】:

    这没用,我只是不想破坏这个问题。

    .controller('ItemsCtrl', function($scope, $http, $q, DSCacheFactory) {
    
            //handle the cache to hold the items lits.
            self.itemsCache = DSCacheFactory.get("itemsCache");
            /*
            *Function to re-refresh the cache after expire
            *re-use the old cache if no internet connection available
            */
            self.itemsCache.setOptions({
                onExpire: function(key, value){
                    getItems()
                        .then(function(){
                        console.log("Items Cache was automatically refreshed", new Date());
                    }, function(){
                        console.log("Errorgetting data put expired item back in the cache", new Date());
                    });
                }
            });
    
            function getItems(){
                var deferred = $q.defer(),
                    cacheKey = "items",
                    itemsData = self.itemsCache.get(cacheKey);
    
                if(itemsData){
                    // if data in the cache dont make  HTTP calls.
                    console.log("Found Data in cache", itemsData);
                    //receive Data from the cache
                    $scope.items = itemsData;
                    deferred.resolve(itemsData);
                }
                else{//if no data in the catch make HTTP calls.
                    //HTTP request to get the items data.
                    $http.get('services/data.json')
                     .success(function(data){
                        console.log("Received data via HTTP");
                        //caching the data received from the http calls.
                        self.itemsCache.put(cacheKey, data);
                        $scope.items = data;
                        deferred.resolve(data);
                    });
                }//end of the else statement
            }
            getItems();
        })
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      相关资源
      最近更新 更多