Jose Pinto
Jose Pinto

Reputation: 93

Cannot read property '$router' of undefined?

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?

Upvotes: 2

Views: 2461

Answers (1)

dagalti
dagalti

Reputation: 1956

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');
    })

Upvotes: 4

Related Questions