0

Inside of a login function I've written in vue I can't assign a value to a variable. It says: "TypeError: Cannot set property 'errorMessage' of undefined".

Outside of this function I can assign a value to it. What am I doing wrong? Note: I Am using AXIOS

import axios from "axios";

export default {
  data() {
    return {
      errorMessage:"",
      username: "",
      password: "",
    };
  },
methods: { 
    login() {
   //This Works!
  // this.errorMessage = "hello";
  axios
    .post("MY-ENDPOINT", {
      email: this.username,
      password: this.password
    })
    .then(function(response) {
      console.log(response);
      if(response.data.error == true) {
        console.log(response.data.errorMessage);
        //This doesn't work??
        this.errorMessage = response.data.errorMessage;

      } else {

      }
    })
    .catch(function(error) {
      console.log(error);
    });
    }
  }
 };

I would expect that the response.data.errorMessage would assign to the errormessage. But it says "TypeError: Cannot set property 'errorMessage' of undefined"inside the "axion .then," outside it works fine.

sirJ
  • 107
  • 1
  • 6
  • 1
    Possible duplicate of [Arrow Functions and This](https://stackoverflow.com/questions/28798330/arrow-functions-and-this) and [Methods in ES6 objects: using arrow functions](https://stackoverflow.com/questions/31095710) – adiga Feb 02 '19 at 08:57
  • Just use an arrow function, as you are binding an incorrect `this` – eljefedelrodeodeljefe Feb 02 '19 at 12:52

0 Answers0