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.