2

I'm trying make so that users can login to page, but is showing me this massage "Cannot read property '$router' of undefined"

My code for login in Vue, it is inside of Export default

import {fb} from "../firebase";
import router from '../router';

export default {
 name: "NavBar",
 components: {...},
 data() {
   return {
    login: false,
    register: false,
    name: null,
    email: null,
    pwd: null,
  };
 },
methods: {
logInner(){
      fb.auth().signInWithEmailAndPassword(this.email, this.pwd)
          .then(function(){
          this.$router.replace('users');
          })
          .catch(function(error) {`enter code here`
              // Handle Errors here.
              var errorCode = error.code;
              var errorMessage = error.message;
              if (errorCode === 'auth/wrong-password') {
                  alert('Wrong password.');
              } else {
                  alert(errorMessage);
              }
              console.log(error);
      });

  },
registering() {
  fb.auth().createUserWithEmailAndPassword(this.email, this.pwd)
    .then((user) =>{
      this.$router.replace('users');
    })
    .catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode == "auth/weak-password") {
        alert("The password is too weak.");
      } else {
        alert(errorMessage);
      }
      console.log(error);
     });
   }
 }
 };

I've tried router.push even router.go dont help. Is someone can helpme please?

1
  • shouldn't be router instead of this.$router ? Commented Mar 22, 2019 at 22:17

1 Answer 1

4

this is not available inside fb.auth() function so Try below. for detailed explanation check this answer : https://stackoverflow.com/a/47148828/9489397

logInner(){
let self = this
      fb.auth().signInWithEmailAndPassword(this.email, this.pwd)
          .then(function(){
          self.$router.replace('users');
          })

and

registering() {
let self = this
  fb.auth().createUserWithEmailAndPassword(this.email, this.pwd)
    .then((user) =>{
      self.$router.replace('users');
    })
Sign up to request clarification or add additional context in comments.

1 Comment

You could also use an arrow function on the callback, to avoid having to declare the "self" variable

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.