[Vuejs]-How set errors from laravel in vuejs

0๐Ÿ‘

โœ…

If you look closely at the error, it says

Cannot set property 'errors' of undefined

So the problem is you are trying to set a property of an undefined object, in this case: this, your Vue component.

An easy and convenient way to solve this problem is to use arrow function instead of a regular function in your Promise.catch() callback. To put simply, a regular function expression has his own this context, where arrow function does not.

Here is a sample of code that should help you understand better:

        new Promise().then()
        .catch((error) => {
           if(error.response && error.response.status == 422) {
              this.errors = error.response.data.errors;
           }
        });  

To learn more about arrow function and the binding of this

Leave a comment