Hi I am geting this error: Uncaught (in promise) TypeError: this.$set is not a function

And here is the code:

 export default { data: function() { return { movies: '' } }, ready: function() { this.showMovies() }, methods: { showMovies: function() { this.$http.get(config.api.url + '/movies').then(function (response) { this.$set('movies', response.data) }) } } } 
1

2 Answers

The reason why this.$set is not a function in your example code is because this doesn't refer to Vue ViewModel instance anymore.

To make code you've posted working, you need to keep reference to it:

export default { data: function() { return { movies: '' } }, ready: function() { this.showMovies() }, methods: { showMovies: function() { var vm = this; // Keep reference to viewmodel object this.$http.get(config.api.url + '/movies').then(function (response) { vm.$set('movies', response.data) }) } } } 
0

in the callback function you're loosing the Vue instance (this), this could be solved by using the arrow function ()=>{...} :

 this.$http.get(config.api.url + '/movies').then((response)=> { this.$set('movies', response.data) }) 

or binding the callback to this :

 this.$http.get(config.api.url + '/movies').then(function (response) { this.$set('movies', response.data) }).bind(this) 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy