Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: this.$set is not a function

Tags:

vue.js

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)
                 })
             }
        }
     }
like image 556
DokiCRO Avatar asked Jan 06 '16 12:01

DokiCRO


People also ask

How do I fix TypeError is not a function?

The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.

Is not a function TypeError?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

What is uncaught TypeError in JavaScript?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.

Is not a function console error?

In short, the "is not a function" error means that the value you're trying to invoke is not a function. The most common reasons this error occurs are: defining a variable with the same name that shadows the original object. calling a method on a value of a different type, e.g. calling the Array.


1 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)
             })
         }
    }
 }
like image 127
Igor Pantović Avatar answered Sep 28 '22 06:09

Igor Pantović