I have a user profile that pulls the user's data from a firestore document. The document returns the data as expected, however, in the console I get 8 identical errors that suggest that the data is undefined.
My code for retrieving the user data is as follows:
TS:
export class UserProfileComponent implements OnInit {
activeUserID: string;
userRef: AngularFirestoreDocument<User>;
userData;
constructor(public afAuth: AngularFireAuth, private db: AngularFirestore) {
this.getActiveUser();
}
ngOnInit() {
}
async getActiveUser(){
const user = await this.afAuth.currentUser;
this.activeUserID = user.uid;
this.getUserId();
this.getUserData();
}
getUserId(){
this.userRef = this.db.collection('users').doc(this.activeUserID);
}
getUserData(){
this.db.collection('users').doc(this.activeUserID).ref.get().then((doc) => {
this.userData = doc.data();
});
}
}
HTML:
<div class="col-md-5">
<h2>{{userData.displayName}}</h2>
<h6>{{userData.username}}</h6>
<p>This is a description about the user.</p>
</div>
Also, if I console.log the userData, it returns undefined depending where it is output.
Screenshot of the console:
core.js:6228 ERROR TypeError: Cannot read property 'displayName' of undefined
at UserProfileComponent_Template (user-profile.component.html:10)
at executeTemplate (core.js:12156)
at refreshView (core.js:11995)
at refreshComponent (core.js:13445)
at refreshChildComponents (core.js:11716)
at refreshView (core.js:12051)
at refreshEmbeddedViews (core.js:13391)
at refreshView (core.js:12022)
at refreshComponent (core.js:13445)
at refreshChildComponents (core.js:11716)