0

I'm attempting to use localforage as the means of storing data, but don't want to rewrite the same code to store and load large arrays of data, so Ive decided to make an object with a prototype which contains said functions. one to setItem and one to getItem. I configure my localforage first, then define my function, then define its prototype.

When I attempt to run this, in the prototype, in setArray, this.forageName is undefined. Why is it undefined, and how do I get it to be recognized?

I want the prototype to recognize when this.forageName is called, it will be called by the instantiated object, and refer to the instantiated object by using 'this'.

localforage.config({
          description: 'Storage medium for caching 2d arrays.'
    });

    /**  
    *   This function is based on Localforage which uses a promise/callback API.
    *   Promise chains make sure that all values are returned before moving onto the next step.
    *
    *   ForageStorage parses through a 2d array and uses localforage to store each array in key-value pairs,
    *   where the first item in each sub-array is the key and the entire sub-array is the value.
    *   @namespace ForageStorage
    *   @param {string} forageName - The name of the localforage object that this object will instantiate.
    *   @param {array[]}  arrayToStore - The array of arrays to parse through and store.
    */
    function ForageStorage(forageName, arrayToStore){
        this.forageName = localforage.createInstance({name: forageName});
        this.arrayToStore = arrayToStore;
        this.retrievedArray = [];
        this.promiseArray = [];
    };


    ForageStorage.prototype = {

        setArray:        function (){
            this.promiseArray = this.arrayToStore.map(function (item) { return this.forageName.setItem(item[0].toString(), item)});
            return Promise.all(promiseArray);
        },
        getArray: function () {
            this.forageName.keys()
            .then(function (keys) {
                this.retrievedArray = keys.map(function (item) {return this.forageName.getItem(item)})
            })
            return Promise.all(retrievedArray);
        },
    };

var forageStorage = new ForageStorage("myStorage",textData);

forageStorage.setArray()
.then(forageStorage.getArray())
.then(console.log(param.map(function () { return item.toString(); })));
  • Just for reference, `alert(this);` shows `[object Window]`, which would've gotten you on the right track to the root of the problem. Basic debugging. –  Jan 10 '17 at 15:09
  • this within the context of a prototype should point to the instantiated object, so your comment just tells me what i already know, that 'this' isn't working as I expected it to. I had a friend look at my code after trying to fix it for a few hours. if I add: var self = this; to the inside of each of the prototype functions, then self can be used within each of the assigned functions to act as 'this'. – David Moreno Jan 10 '17 at 15:52
  • You're not using `this` in the context of a prototype, you're using `this` in an anonymous function which is an argument in a function call in the context of a prototype. It's a well-known issue with multiple solutions plastered all over the net, *that's* the point of my comment. Plus, you were asking "why is it undefined", which means you didn't realize that `this` was no longer pointing at the object. –  Jan 10 '17 at 16:34

0 Answers0