Reputation: 21
Every time I logout from my app, I get
Cannot read property 'uid' of null
from console. it leads reference to below code:
constructor( private firestore: AngularFirestore, private auth: AuthService, )
{
firebase.auth().onAuthStateChanged(user => {
if (this.currentUser == user) {
this.currentUser = firebase.auth().currentUser;
this.vendor = firebase.firestore().doc(`/Products/${this.currentUser.uid}`);
}
});
Upvotes: 0
Views: 404
Reputation: 31
you can use uid for optional chaining
firebase.firestore().doc(`/Products/${this.currentUser?.uid}`)
or another solution is when you logout from the system set current user is null or empty object
setCurrentUser({})
Upvotes: 0
Reputation: 598837
The onAuthStateChanged
callback will fire whenever the user's authentication state changes, so whenever they sign in, or they're signed out. In the latter case, the user
will be null
, which you don't handle correctly in your code right now.
This looks more correct:
firebase.auth().onAuthStateChanged(user => {
this.currentUser = firebase.auth().currentUser;
if (this.currentUser) {
this.vendor = firebase.firestore().doc(`/Products/${this.currentUser.uid}`);
}
});
Upvotes: 1