jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

cannot use in operator to search for <key> in null #207

Closed robshep closed 8 years ago

robshep commented 8 years ago

Occasionally, I get the following error:

TypeError: Cannot use 'in' operator to search for 'AccountBalance' in null
    at Object.get (http://localhost:7999/bower_components/angular-cache/dist/angular-cache.js:359:24)
    at Object.getAccountBalance (http://localhost:7999/app/components/common/profileCache.service.js:27:21)

Any ideas?

versions:

angular-cache: 4.4.2 angular-core: 1.4.8

Here is my cache config in the service:

/** @ngInject */
  function profileService($http, $log, $rootScope, CacheFactory, $q) 
  {
      var profileCache = CacheFactory.get('profileCache')
      if (!profileCache) {
        profileCache = CacheFactory.createCache('profileCache', {
            deleteOnExpire: 'aggressive',
            recycleFreq: 120 * 60 * 1000,
            storageMode: 'localStorage',
        });
        $log.info("Creating profileCache")
      }

      function getAccountBalance(force)
      {
            var deferred = $q.defer();

Line 27 --> if (  !!(profileCache.get("AccountBalance"))  && !force ) {
                deferred.resolve(profileCache.get("AccountBalance"));
            } else {
                $http.get('/api/v1/profile/balance').then(function (resp) {
                    setAccountBalance(resp.data)
                    deferred.resolve(resp.data);
                });
            }
            return deferred.promise;
      }

    return {
       getAccountBalance: getAccountBalance
    }
}
jmdobry commented 8 years ago

Are you destroying the cache anywhere?

robshep commented 8 years ago

Actually I was just grokking the source and looking at $$data thinking.... hmmm it only get nullified on destroy... just looking.

robshep commented 8 years ago

Found it. A logout event was .destroy()ing the cache. This leads me to believe it only occurs when a user logs out, then logs in without a page refresh (and app+service build). Changed to removeAll() to keep the cache alive. Please close this off - Sorry about that. My bad. Thx for the quick response, and for the module!.