【问题标题】:how to access nested firebase collection with its parent collection如何使用其父集合访问嵌套的 Firebase 集合
【发布时间】:2020-11-30 02:44:33
【问题描述】:

我正在开发一个论坛应用程序,用户可以在其中提出问题并获得答案(每个答案都有自己的喜欢) 我想要的是当我访问答案时,我想得到它的子集合。但我不知道如何访问“喜欢”集合。我有 google 并找到了收集组查询的方法,但对 firebase 没有足够的了解。有人能帮我获得“喜欢”吗

getcomments(id) {
  this.commentcollection = this.questioncollection.doc(id).collection<Comment>('answer');
  this.comments = this.commentcollection
    .snapshotChanges()
    .pipe(map(changes =>{
      return changes.map(a=>{
        const data = a.payload.doc.data() as Question;
        data.id = a.payload.doc.id;
        return data;
      })
    }));
  return this.comments;
}

这里是收集评论,

<li *ngFor="let comment of selectedComment |async">
  <div class="comment-main-level">
    <div class="comment-box">
      <div class="comment-head">
        <span>{{comment.time.toDate() | date: 'dd MMM hh:mm'}}</span>
        <i>Likes</i>
        <i (click)="likepost(comment.userID,comment.id)" class="fa fa-thumbs-up"></i>
        <i (click)="editComment($event, comment)" *ngIf="comment.userID==authService.currentUserId" class="fa fa-pencil-alt">  </i>
        <i (click)="deleteComment(comment.id)" *ngIf="comment.userID==authService.currentUserId" class="fa fa-trash-alt"></i>
      </div>
      <div class="comment-content">
        {{comment.answerBody}}
      </div>

这是我显示评论的 html,我想在标签处显示每条评论。 我怎样才能得到喜欢的收藏。

【问题讨论】:

  • 喜欢不需要自己的实体/收藏。只需向评论实体添加一个属性(类型 int)...
  • 如果用户点击喜欢的数量,我想显示一个喜欢的列表

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


【解决方案1】:

在我看来,您可以像访问getcomment 函数中的answer 集合一样执行此操作。文档的嵌套集合可以通过collection 方法访问,集合中的嵌套文档可以通过doc 方法访问。这可以重新开始,直到您到达结构的最后一层 - 就像这样:

parentCollectionReference.doc(<docid>).collection(<nestedCollectionId>).doc(<nestedDocId>).collection(<collectionNestedInNestedDoc>).doc... etc.

我了解您将拥有用户将选择的答案的文档 ID。让我们说它的anwserId。您还拥有对answer 集合的collectionReference,因为您已经在getcomment 中访问它——假设它是this.commentcollection。逻辑将与getcomment 方法中的完全相同。您必须参考正确的子集合的主要区别。伪代码如下所示:

this.likeCollection = this.commentcollection.doc('anwserId').collection<Likes>('likes');

其余的都可以与getcomment函数类似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    相关资源
    最近更新 更多