0

I am having a few problems with an Angular project im working on.

I decided to use Angular7 with Firebase (AngularFire v5)

In my AuthService

export class AuthService {
private authState: Observable<firebase.User>;
public currentUser: firebase.User;
private profileDoc: AngularFirestoreDocument<Item>;
private profile$: Observable<Item>;

export class AuthService {
private authState: Observable<firebase.User>;
public currentUser: firebase.User;
private profileDoc: AngularFirestoreDocument<Item>;
private profile$: Observable<Item>;

constructor(public afAuth: AngularFireAuth, private afs: AngularFirestore) {
    this.authState = this.afAuth.authState;
    this.afAuth.authState.subscribe((user: firebase.User) => {
        this.currentUser = user;
        if(user !== null && user.uid) {
            this.profileDoc = this.afs.doc<Item>('users/' + user.uid);
            this.profile$ = this.profileDoc.valueChanges();
        }
    });
}

get profile() : Observable<Item> {
    return this.profile$;
}
}

I am trying to get a firestore reference to the users profile when they have logged in, and on the frontend in a component show the firstname and lastname of the user.

But when i try and access the profile to subscribe to it:

 this._auth.profile.subscribe((profile) => {
        console.log("Profile", profile);
 });

It gives me errors.

ERROR TypeError: Cannot read property 'subscribe' of undefined

1 Answers1

0

You can create a function where in you use the AngularFireAuth service to directly get the uid of the user and then make a query to the firestore something like this

constructor(private afAuth: AngularFireAuth, private afs: AngularFireStore) {}

getProfile() {
   const userId = this.afAuth.auth.currentUser.uid;
   const doc = this.afs.doc('path' + userId );
   return doc.valueChanges();
   // then subscribe to this in the component
}

hope it helps

Madara Uchiha
  • 37
  • 2
  • 9