scotttrinh / angular-localForage

Angular service & directive for https://github.com/mozilla/localForage (Offline storage, improved.)
MIT License
651 stars 86 forks source link

Cannot read property 'transaction' of undefined #101

Open RyanP13 opened 8 years ago

RyanP13 commented 8 years ago

I am getting this issue in my angular application.

The local forage provider is configured in the config block like so:

            $localForageProvider.config({
                driver: localforage.LOCALSTORAGE,
                name: 'ttn',
                storeName: 'keyvaluepairs'
            });

Once a user is logged in there is a browser refresh timer which triggers on an hourly basis.

When that happens the config block will run again and throw the following error in the localforage JS file because once logged in we try to resolve the logged in user on that route. It seems from localforage JS that in line 1186:

var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly')

The db is not yet defined. When i check that the data is there in local storage from the window object it is definitely stored there.

Is this expected behaviour?

Caligone commented 8 years ago

Same issue here. It happens when I use $localforage on a promisified resolved service.

CreatedByVictor commented 8 years ago

Same problem here with a very similar setup to RyanP13.

RyanP13 commented 8 years ago

For what it is worth i solved this in a hacky way by checking the window.localStorage object to see if my storage key was present. If not found then define the store. Not pretty:

        function configureLocalForage(){

            var isStoreDefined = false;

            for (var key in window.localStorage){
                if(key.match(/^ttn\/[a-z0-9]*/gmi) !== null){
                    isStoreDefined = true;
                }
            }

            if(!isStoreDefined){
                $localForageProvider.config({
                    driver: localforage.LOCALSTORAGE,
                    name: 'ttn',
                    storeName: 'keyvaluepairs'
                });
            }

        }
scotttrinh commented 6 years ago

Can someone make a minimal reproduction case for this? I'm not sure I follow the issue, as I've never seen this in my own usage.