vue.jslocalforage

Vue localforage no update


I'm using localforage, but I can't figure out why this.emails is not updated outside the localforage. I get no errors in console.

localforage.getItem("emails").then(function (value) {
                        this.emails = value
                        console.log(value)   // shows correct value
                        console.log(this.emails) // shows correct value
                    }).catch(function (err) {
                        console.log(err); // no error is thrown
});

Solution

  • Functions have a this scope. So when you're setting this.emails, you're setting the function's this.emails instead of your Vue component.

    You could use bind to deal with this, but the easier way would be to just use an anonymous function in your then, like this:

    localforage.getItem("emails").then((value) => {
        this.emails = value
    })