Cannot push the page after successful firebase email password auth

Tried to push dashboardPage after successful auth, but throws error saying nav is n’t defied. Any help?

    export class AdminPage {
    	static get parameters(){
    		return [[NavController],[UserData]];
    	}
      constructor(nav, userData) {
      	this.nav = nav;
      	this.userData = userData;
      	this.login = {};
      	this.submitted = false;

      	this.firebaseUrl = 'https://examplexo.firebaseio.com/';
  }
  onLogin(form){

  	this.submitted = true;
  	if(form.valid){
  		this.userData.login();
  		var ref = new Firebase(this.firebaseUrl);
  		ref.authWithPassword({
  			email : form.controls.username.value,
  			password: form.controls.password.value
  		}, function(error, authData){
  			if(error){
  				console.log(error);
  			} else {
  				this.nav.push(DashboardPage);
  				console.log(authData);
  			}
  		});
  	}
  	}

Just a guess, I don’t think this is the AdminPage when you call it in the callback of the authWithPassword function. To check you can add this in the beginning of the onLogin function

var self = this; and use self.nav.push(DashBoardPage);

In typescript you can us the => syntax to pass along the scope to your function so you dont have to use self. I think you can use it with js as well but im not sure. You can correct me if I am wrong :slight_smile:

1 Like

Thanks @dtaalbers. It actually worked.

Nice to hear it helped!

Did you use var self = this? Or the => (fat arrow) syntax?

  ref.authWithPassword({
  	email : form.controls.username.value,
  	password: form.controls.password.value
  }, (error, authData) => {
        if(error){
  	   console.log(error);
  	} else {
  	   this.nav.push(DashboardPage); // this should work now
  	   console.log(authData);
  	}
  }

This example is untested though, but it should work with ES6. With this you dont have to declare

var self = this;

because the => will pass the AdminPage context to your function; which is much more prettier in my opinion :slight_smile: