Home > Software engineering >  Vue localforage no update
Vue localforage no update

Time:02-16

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
});

CodePudding user response:

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
})
  • Related