Noman
Noman

Reputation: 887

Angular2 - my service is null

import { Injectable } from '@angular/core';

@Injectable()
export class UserProfileProvider {
    private uid: string;

    constructor() {
    }
    function1() {}

}

Above is my service. I have it in the provider in the app.module.ts:

  providers: [
    .
    .
    .
    UserProfileProvider,
  ]

In app.component.ts I'm injecting it like:

constructor(public userProfile: UserProfileProvider)

But userProfile is null:

platform.ready().then(() => {
  statusBar.styleDefault();
  splashScreen.hide();
  if(this.userProfile == null) {
        // true
        console.log("userProfile == null");
    }
});

Update 1: But userProfile is not null on the home.ts

Upvotes: 0

Views: 70

Answers (1)

Noman
Noman

Reputation: 887

platform.ready().then(() => {

  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  statusBar.styleDefault();
  splashScreen.hide();

  this.subscription = this.afAuth.authState
    .catch((error: any) => {
      console.log("google login failed, error: ", error);
      this.rootPage = LoginPage;
      this.userProfile = null;
      return Observable.empty();
    })
    .subscribe((user: firebase.User) => {
      if (user && user.uid) {
        this.rootPage = HomePage;
        this.events.publish('user:signedIn', user.uid);
        if(this.userProfile == null) {
          console.log("userProfile == null");

        } else {
          this.userProfile.setUid(user.uid);
        }
        // this.userProfile.setUid(user.uid);
      } else {
        this.rootPage = LoginPage;
        // this.userProfile = null; <== culprit
      }
    });
});

On clearing the browser cache the user would be null and then I was setting this.userProfile to null and then when I tried to login it would be null.

Upvotes: 1

Related Questions