首页 > 解决方案 > 如何仅获取在 Firestore 上注册的相关数据

问题描述

我想检查“userId”并只获取在 Firestore 上注册的相关数据,但在 Firestore 上注册的所有内容都会打印出来。我不知道要修复哪个部分。我试图根据建议更改它,但它没有改变。对不起,我需要你的帮助。

应用程序组件打字稿代码是,

constructor(...) {
  this.userId = authService.currentUserId; // for load uid data 
  this.userCollection = afs.collection<User>('users/'+this.userId);
  this.user$ = this.userCollection.snapshotChanges().pipe(
      map(actions => {
      return actions.map(a => {
          const data = a.payload.doc.data() as User;
          const id = a.payload.doc.id;
          id == this.userId;
          return { id, ...data };
      });
      })
  );
} 

<ion-item *ngFor="let data of user$ | async">
<ion-label>
{{data.myName}}
<p>{{data.userId}}</p>
</ion-label>
</ion-item>

标签: angulartypescriptfirebase

解决方案


尝试这个。

    this.user$ = afs.doc<User>('users/' + this.userId).snapshotChanges().pipe(
      map(action => {
        const data = action.payload.doc.data() as User;
        const id = action.payload.doc.id;
        return { id, ...data };
      })
    );

推荐阅读