Angular-无法从Firestore获取单个文档

时间:2018-07-05 14:48:34

标签: javascript angular typescript firebase google-cloud-firestore

我正在尝试列出用户列表,这是Firestore中的一个集合,然后该用户将能够单击用户,并被重定向到包含更多数据的用户个人资料页面。我能够显示数据;但是,对于集合中的所有其他文档,我都收到一个控制台错误,提示找不到属性。

user-profile.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; 
  }

user-profile.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)

0 个答案:

没有答案