1

I'm creating login validation in Vue.js but the error message is not displaying and it gives me the error:

Property or method "error" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

Any help?

Template:

<template>
  <div class="container" width="900">
    <div class="row justify-content-center"  style="margin-top: 10px;">
      <div class="col-5">
        <div v-if="error" class="alert alert-danger" role="alert">
          {{error}}
        </div>
        <div class="card" >
          <div class="card-text">
            <div class="form-group" @keyup.enter="logItIn">
              <input class="form-control"
              v-model="login.email"
              label="Email"
              placeholder="Email Address"
              required
              >   <br>
              <input class="form-control"
              v-model="login.password"
              label="Password"
              type="password"
              placeholder="Password"
              required>
            </div>
          </div>
          <button type="button" class="btn btn-secondary"  @click='logItIn'>Login</button>
        </div>
      </div>
    </div>
  </div>
</template>

Script:

import axios from 'axios'
export default {
  data() {
    return {
      login: {
        email:"",
        password:"",
        error: ""
      }
    }
  },
  methods: {
    async logItIn() {
      try {
        axios.post('https://odevin-api.herokuapp.com/login',this.login)
          .then(response => {
            console.log(response)
            let newToken=response.data.response.token;
            window.token=newToken;
            let user=response.data.response.user;   //response
            localStorage.setItem('token',newToken);
            localStorage.setItem('user',JSON.stringify(user));
            window.axios.defaults.params={api_token:newToken}
            // Event.$emit('login',user);
            this.$router.push('/');
          });
      } catch(e) {
        this.error = 'invalid user name or password';
      }
    }
  }
}
1
  • You're using it as error but you defined it as login.error. Maybe you meant to move it outside of the login object in data. Commented Dec 18, 2020 at 18:08

1 Answer 1

2

you referenced {{ error }} in your template but in your data object, error is a property of login object. so vue can't find it properly.

either change the usage in your template to {{ login.error }} or define it in your data object like this:

data() {
  return {
    error: '',
    login: {
      email: '',
      password: '',
    },
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

i treid this but now it doesn't show the error message i set "invalid user name or password"
So you are passed the error in your question, now you have to check if the error is updated correctly or not! did you change the template binding or the data object?
the data object
I think there might be some issue with logItIn function, you used async keyword but I can't see any await keyword in it! I doubt this is the right syntax, maybe catch block doesn't get to run at all. read this link maybe it helps (only the first part that describes a login function in vue using async ... await syntax), also you can put a console.log in the catch block to see if it runs or not: stackoverflow.com/questions/65343792/vue-login-function/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.