如何使用 lodash _.filter() 过滤 firestore 集合?

How can I filter a firestore collection using lodash _.filter()?

我正在尝试过滤从 firebase firestore 检索到的集合(在 angularfire2 的帮助下)。问题是我没有在过滤列表中得到任何东西。

这是我的代码:

companies.component.ts

import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable, BehaviorSubject, combineLatest } from 'rxjs';
import { Company } from '../../shared/models';
import { ReactiveFormsModule, FormGroup, FormBuilder, Validators } from '@angular/forms';
import * as _ from 'lodash';

import { Subject } from 'rxjs';

import { switchMap, map} from 'rxjs/operators';


@Component({
  selector: 'app-companies',
  templateUrl: './companies.component.html',
  styleUrls: ['./companies.component.scss']
})

export class CompaniesComponent implements OnInit {

  filters = {}
  private companiesCollection: AngularFirestoreCollection<Company>;
  companies: any;
  filteredCompanies: any;

  constructor(private readonly afs: AngularFirestore) {
    this.companiesCollection = afs.collection<Company>('companies');
    this.getCompanies().subscribe(() => {
            this.applyFilters();
    });
  }

  ngOnInit() {
  }

  getCompanies() {
    return this.companies = this.afs.collection<Company>('companies').valueChanges();
  }
  /// filter properties that resolve to true
  filterBoolean(property: string, rule: boolean) {
    if (!rule) this.removeFilter(property)
    else {
      this.filters[property] = val => val
      this.applyFilters()
    }
  }

  private applyFilters() {
     this.filteredCompanies = _.filter(this.companies, _.conforms(this.filters) )
  }

companies.component.html

<div class="field">
  <input type="checkbox" (change)="filterBoolean('firstCondition', $event.target.checked)"> True or false 1?
</div>
<div class="field">
  <input type="checkbox" (change)="filterBoolean('secondCondition', $event.target.checked)"> True or false 2?
</div>
<div class="field">
  <input type="checkbox" (change)="filterBoolean('secondCondition', $event.target.checked)"> True or false 3?
</div>


<!-- (WORKING): Displays all the companies names -->
<div *ngFor="let company of companies | async">
  <h6>{{ company.name }}</h6>
</div>

<!-- (NOT WORKING): Displays all the filtered companies -->
<div *ngFor="let filteredCompany of filteredCompanies">
  <h6>{{ filteredCompany.name }}</h6>
</div>

我在用什么:

Angular6、angularfire2(v.5)、lodash(v.4)。

帮助:

我是否在某种程度上不匹配数据结构? 任何人都可以解决这个问题?

我正在关注 this tutorial from Jeff Delaney 并尝试在文章中的 "Option 1 - Client Side Filtering" 下适应 firestore 而不是实时数据库。

任何可以使我更接近目标的帮助将不胜感激。

用解决方案编辑:

DeborahK 的回答帮我解决了这个问题。

在构造函数中更改为:

this.getCompanies().subscribe(companies => {
        this.companies = companies; // <== Added this line
        this.applyFilters();
});

而函数getCompanies()为:

// Now only returns the retrieved collection    
getCompanies() {
        return this.afs.collection<Company('companies').valueChanges();
}

this.companies设置在哪里?看起来您正在将它传递到过滤器中,但没有在任何地方设置它。

this.filteredCompanies = _.filter(this.companies, _.conforms(this.filters) )

请注意,在上面的代码中,您使用 this.companies 作为过滤源。如果未设置,则不会有任何过滤公司。

这个代码应该:

this.getCompanies().subscribe(() => {
        this.applyFilters();
});

也许是这个?

this.getCompanies().subscribe(companies => {
        this.companies = companies;
        this.applyFilters();
});