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

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?

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.

1 Like

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

Hi

Yes. Never use function. Only fat arrow.

Works like a breeze.

Rgdz

Tom

1 Like