Firestore搜索过滤(无阿尔戈利亚)

时间:2018-08-07 12:20:27

标签: angular typescript google-cloud-firestore

我正在尝试使用Angular 6过滤来自Firestore的集合,但是它似乎无法正常工作。我能够过滤本地JSON列表(请参见here),但不能过滤来自Firestore的文档集合(请参见here)。我的代码如下所示:

本地方式

itemCtrl: FormControl;
filteredItems: Observable<any[]>

items: Item[] = 
[
    {
        name: 'First Item',
        city: 'Sub',
        state: 'One'
    },
    {
        name: 'Second Item',
        city: 'Sub',
        state: 'Two'
    },
         etc...
]

ngOnInit(){
    this.filterItems();
}

filterItems(){
    this.itemCtrl = new FormControl();
    this.filteredItems= this.itemCtrl.valueChanges.pipe(
        startWith(''),
        map(name => name ? this.filteritems2(name) : this.items.slice()));
}

filterItems2(name: string) {
    return this.items.filter(item=>item.name.toLowerCase().indexOf(name.toLowerCase())===0)
}

Firestore Way

ngOnInit(){
    this.itemsCollection = this.afs.collection('Items', ref => { return ref.limit(4) });
    this.items = this.itemsCollection.valueChanges();
}

0 个答案:

没有答案