javascript - Angular - 从 Firestore 获取单个文档时遇到问题
问题描述
我正在尝试创建一个用户列表,这是 Firestore 中的一个集合,然后用户将能够单击用户并被重定向到具有更多数据的用户个人资料页面。我能够显示数据;但是,对于集合中的每个其他文档,我都收到一个控制台错误,说找不到属性。
用户配置文件.ts:
ngOnInit() {
//get user id from route
this.uid = this.route.snapshot.params['uid'];
//get user document reference from firestore using auth service
this.authService.getUserDocById(this.uid).get().then(doc =>{
this.user = doc.data();
}
);
if(this.uid == this.authService.getFirebaseUser().uid){
this.isOwnProfile = true;
}
else{
this.isOwnProfile = false;
}
}
auth.service.ts:
getUserDocById(uid: string)
{
var path = ('users/' + uid);
return this.afs.doc(path).ref;
}
用户配置文件.component.html:
<div class="imagewrapper">
<div [ngClass]="{ 'isOwnImage' : isOwnProfile }" [ngStyle]="{ 'background-image': 'url(' + user.profileImageUrl + ')' }" class="profileImage"></div>
除了我当前正在查看的用户之外,无论集合中有多少用户,我都会收到此控制台错误:
TypeError: Cannot read property 'profileImageUrl' of undefined
at Object.eval [as updateDirectives] (UserProfileComponent.html:2)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:10853)
at checkAndUpdateView (core.js:10250)
at callViewAction (core.js:10491)
at execComponentViewsAction (core.js:10433)
at checkAndUpdateView (core.js:10256)
at callViewAction (core.js:10491)
at execEmbeddedViewsAction (core.js:10454)
at checkAndUpdateView (core.js:10251)
at callViewAction (core.js:10491)
解决方案
推荐阅读
- sql - SQL:内存不足错误 | Heroku Postgres + Hasura GraphQL
- accessibility - 屏幕阅读器在元素展开时阅读内容
- java - 如何使用 Java 中的 EditorConfig 在 IntelliJ IDEA 中关闭其自己的行上的右括号?
- html - 如何在不考虑地址栏的情况下正确计算移动设备上的视口高度?
- ios - 无主变量和异步函数
- r - 用组的第一次观察替换所有值
- c - 在递归函数中使用静态
- java - AWS CDK Vpc.GetPublicSubnets() 返回错误数量的公有子网
- python - Python - 当小时值大于 24 时将字符串解析为 timedelta
- python - django 在未登录的情况下使用管理小部件