简体   繁体   English

按文档字段过滤 Firestore 集合

[英]Filtering the firestore collection by a document field

I have three collections: users, doctors and reviews.我有三个 collections:用户、医生和评论。

In my component.ts I have two arrays:在我的 component.ts 中有两个 arrays:

Doctors: Doctor[];
Reviews: Review[];

I returned in a *ngFor all the elements of the Doctors .我返回了*ngFor all the elements of the Doctors

  • doctor elements医生元素

医生元素

  • review collection评论集

评论集

  • doctor collection医生集合

医生集合

In the button Show Reviews I call a function getDoctorReviews(doctorID) and in this function I return the data of the review collection filtering the data in this way:在按钮显示评论中,我调用了 function getDoctorReviews(doctorID) 并在此 function 中返回评论集合的数据,以这种方式过滤数据:

collection("reviews", ref => ref.where("doctorID", "==", this.doctorID);

and I receive undefined我收到未定义的

I tried in this way:我试过这样:

import { User } from './../model/user.model';
import { DoctorInfoService } from '../services/doctor-info.service';
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Data } from "@angular/router";
import { AngularFirestore, AngularFirestoreDocument, AngularFirestoreCollection } from '@angular/fire/firestore';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';

interface Doctor {
  doctorID: string;
  category: string;
  doctorName: string;
  reviewsNumber: number;
}

interface Review {
  doctorID: string;
  doctorName: string;
  userID: string;
  userName: String;
  rating: string;
  message: string;

}

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
  Doctors: Doctor[];
  Reviews: Review[];
  Users: User[];

  doctorDocument: AngularFirestoreDocument<Doctor>;
  doctorCollection: AngularFirestoreCollection<Doctor>;

  @Input() doctorID: string;

  constructor(
    private db: AngularFirestore) {

  }

  ngOnInit(): void {

    this.db
      .collection("doctors")
      .snapshotChanges()
      .subscribe(res => {
        this.Doctors = res.map(e => {
          return {
            id: e.payload.doc.id,
            ...e.payload.doc.data() as Doctor
          }
        })
      });
  }

  showReviews: boolean = false;

  getDoctorReviews() {
    this.showReviews = true;
    this.db
      .collection("reviews", ref => ref.where("doctorID", "==", this.doctorID))
      .snapshotChanges()
      .subscribe(res => {
        this.Reviews = res.map(e => {
          return {
            id: e.payload.doc.id,
            ...e.payload.doc.data() as Review
          }
        })
      });
  }

  hideDoctorReviews() {
    this.showReviews = false;
  }
}
<div *ngFor="let doctor of Doctors">
  <div class="list-group">
    <a class="list-group-item list-group-item-action flex-column align-items-start">
      <div class="d-flex w-100 justify-content-between">
        <h5 class="mb-2">{{ doctor.doctorID }}</h5>
      </div>
      <p class="mb-2">Category {{ doctor.category }}</p>
      <p class="mb-2">Rating </p>
      <button class='btn btn-primary' (click)="getDoctorReviews(doctorID)">Show Reviews</button>
      <button class='btn btn-secondary' (click)="hideDoctorReviews()">Hide</button>
    </a>
  </div>
</div>


<div *ngIf="showReviews" class="list-group">
  <div *ngFor="let review of Reviews" class="row">
    <a class="list-group-item list-group-item-action flex-column align-items-start">
      <div class="d-flex w-100 justify-content-between">
        <h5 class="mb-2">{{ review.doctorName }}</h5>
      </div>
      <p class="mb-2">Rating : {{ review.rating }}</p>
      <p class="mb-2">{{ review.userName }}</p>
      <p class="mb-2">{{ review.message }}</p>
    </a>
  </div>
</div>

There should be a way to show only the documents from reviews depending on the doctor ID field value.应该有一种方法可以根据医生 ID 字段值仅显示评论中的文档。

I appreciate any help.我很感激任何帮助。 Thanks谢谢

You have @Input() doctorID: string;你有@Input() doctorID: string; in your component, which suggests that the containing component will pass a value for the doctorID field.在您的组件中,这表明包含组件将传递doctorID字段的值。 However, your click handler is calling getDoctorReviews(doctorID) .但是,您的点击处理程序正在调用getDoctorReviews(doctorID) That method refers to this.doctorID but you don't appear to be setting that field anywhere.该方法引用this.doctorID但您似乎没有在任何地方设置该字段。

Remove the component doctorID field, then pass doctorID (without this. ) to the where .删除组件doctorID字段,然后将doctorID (不带this. )传递给where

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM