Typeerror: This. $Set Is Not a Function

Typeerror: This. $Set Is Not a Function

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

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

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.