Ionic: Uncaught (in promise): TypeError: Cannot read property of null


#1

Hi,

in Ionic i have imported the native facebook plugin.
I have a Page:

import { Component } from ‘@angular/core’;
import { NavController, NavParams } from ‘ionic-angular’;
import { LoginEmailPage } from ‘…/login-email/login-email’;
import { AuthService } from ‘…/…/providers/auth-service’;
import { Facebook } from ‘ionic-native’;



export class LoginPage {
constructor(public navCtrl: NavController, public navParams: NavParams) {}

 Facebook.login(["public_profile", "email", "user_location", "user_birthday"])
.then(function(response){
  this.navCtrl.push(LoginEmailPage);
  }
})

The login work correctly but i have the following error:

EXCEPTION: Uncaught (in promise): TypeError: Cannot read property ‘navCtrl’ of null
TypeError: Cannot read property ‘navCtrl’ of null

What’s the problem?


Wifi Scanner with wifiwizard
#2

Replace function with arrow function instead.

When you write function() { } instead of an arrow function () => { } … then whenever you call this it would refer to the scope of function(){ } and not the class that you’re in.


#3

So we need to use () => { } for acces to this.navctrl ?


#4

Hi

Yes. Never use function. Only fat arrow.

Works like a breeze.

Rgdz

Tom