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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.