[Vuejs]-TypeError when trying to assign axios response to data variable in Vue component

0👍

Simple answer: I should have been using an arrow function in the first promise.

Change:

.then(function (response) {
          console.log(response.data.word);
          this.randomWord = response.data.word;
        })

to:

.then((response) => {
          this.randomWord = response.data.word;
        })

A better explanation can be found here: Vue – Cannot set property of undefined in promise

Leave a comment