У меня есть профиль пользователя, который извлекает данные пользователя из документа firestore. Документ возвращает данные, как ожидалось, однако в консоли я получаю 8 идентичных ошибок, которые предполагают, что данные не определены.
Мой код для получения пользовательских данных следующий:
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>
Кроме того, если я console.log userData, он возвращает undefined в зависимости от того, где он выводится.
Скриншот консоль:
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)