I have a simple code based on Vue.js:
const app = new Vue({
el: 'vue-app',
data: {
displayedBooks: {}
},
created() {
fetch('/library').then(response => response.json())
.then((data) => this.data.displayedBooks = data);
}
});
But I got an exception:
Uncaught (in promise) TypeError: Cannot set property 'displayedBooks' of undefined at fetch.then.then (main.js:8)
Why this simple code is not works well?
this.displayedBooks, notthis.data.displayedBooks. Everything in your Vuedataparameter gets attached tothisdirectly.this.displayedBooks = data:)this.displayedBooksand don't forget about store context withvar self = thisincreated()function