@ionic-native/Facebook undefined - not being passed to constructor?

I’m running my app on my android device, and when I click on my Facebook login button, I get the error

Error: Uncaught (in promise): TypeError: Cannot read property 'fb' of undefined

My page:

...
import { Facebook } from '@ionic-native/facebook';
...
constructor(public fb: Facebook, ...
...
LoginFacebook(event, item)
{
    console.log("click facebook login");
    this.fb.getLoginStatus()
...

I’ve followed every suggestion I can google. I’ve removed the cordova-plugin-facebook4 and then added it back, I’ve installed @ionic-native/Facebook correctly as far as I can tell. I’ve imported Facebook into app.module and added it to Providers.

I don’t know why it thinks this.fb is undefined?

Hmmm. What does your button code look like?

it fails right at the start, at the this.fb.getLoginStatus:

LoginFacebook(event, item)
	{
		this.ShowLoading("Signing In...");
		
		console.log("click facebook login");
		this.fb.getLoginStatus()
					.then(function(response)
					{
						if (response.status == "connected")
						{
							this.XyzApi.GetSocialAccessToken(IdentityProvider.Facebook, response.authResponse.userID)
										.map((tokenSaved: boolean) =>
										{
											if (!tokenSaved)
											{
												//They've authorized the app, but we couldn't get an access token for them, means somehow registration failed
												this.CreateFacebookUser(response);
											}
											else
											{
												//We have everything we need to show their dashboard
												this.navCtrl.setRoot(DashboardPage);
											}
										});
							return;
						}

						//It's not_authorized or unknown, so login
						this.fb.login(this.XyzApi.FB_PERMISSIONS)
								.then(function (response)
								{
									this.CreateFacebookUser(response);
								}, 
								function (error)
								{
									console.log(error);
								});
					});
	}

Ah, sorry. I meant in your template/HTML.

No worries:

<button ion-button block icon-left color="facebook" (tap)="LoginFacebook($event)" class="labeled"><ion-icon ios="logo-facebook" md="logo-facebook" name="logo-facebook"></ion-icon>Sign In With Facebook</button>

Is there any particular reason you’re using tap over click? I’m not sure if (or why) using tap would break things, but normally click is used.

You missed to import the plugin in you app.module.ts

Please read the documentations carefully

1 Like

Never type the word “function” inside the body of one. Always use arrow functions instead. I suspect you are losing execution context.

ugh, this was it. the stupid sample I copied it from used a function and I’m still new to this and didn’t notice. Thanks!