localforage

ReferenceError: localforage is not defined | localforage.getItem()

I am getting an error (ReferenceError: localforage is not defined) when using the below code.

app.service('cacheService', function () {
    return {
        set: function (key, value, expireTimeInSeconds) {
            return localforage.setItem(key, {
                data: value,
                timestamp: new Date().getTime(),
                expireTimeInMilliseconds: expireTimeInSeconds * 1000
            })
        },
        get: function (key) {
            return localforage.getItem(key).then(function (item) {
                if (!item || new Date().getTime() > (item.timestamp + item.expireTimeInMilliseconds)) {
                    return null
                } else {
                    return item.data
                }
            })
        }
    }
});

Error:  ReferenceError: localforage is not defineds

We need to add localforage to our project as well: https://github.com/mozilla/localForage
angular-localforage is just the angular wrapper.

Solution 1:  To use localForage, just drop a single JavaScript file into your page:
<script src="localforage/dist/localforage.js"></script>
<script>localforage.getItem('something', myCallback);</script>

Solution 2:
Download the latest localForage from GitHub, or install with npm:
npm install localforage

Solution 3:
Add the reference on the HTML page,
<script src="https://cdn.rawgit.com/mozilla/localForage/master/dist/localforage.js"></script>

Live example, https://codepen.io/thgreasi/pen/ojYKeE
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^