简体   繁体   English

基于angularfirestore集合中的子集合的字段值进行搜索

[英]Search based on a field value of subcollection in angularfirestore collection

Trying to search in a list based on the name field of firebase database using angularfirestore. 尝试使用angularfirestore基于firebase数据库的名称字段搜索列表。 It does not even show any error in console. 它甚至没有在控制台中显示任何错误。 the result returning from the code in service is an empty array. 从服务中的代码返回的结果是一个空数组。 I filtered and map the payload data in constructor to get the async and access the nested objects in the database 我过滤并在构造函数中映射有效负载数据以获取异步并访问数据库中的嵌套对象

firebase系列

list.html: list.html:

    <input type="search" 
          class="form-control"
          [(ngModel)]="searchValue"
          [ngModelOptions]="{standalone: true}"
          (keyup)="searchMasjid()"
          placeholder="Name...">
  <div *ngIf="masjids$ | async; let masjids; else loading">
    <div class="card mb-1" *ngFor="let masjid of masjids">
      <div class="card-body d-flex justify-content-between">
        <div>
          <h5 class="mb-1">{{masjid.masjidInfo.jagah}}-{{masjid.masjidInfo.name}}</h5>
          <small>{{masjid.masjidInfo.address}}</small>
        </div>

list.ts: list.ts:

export class MasjidListComponent{
  public masjids$: Observable<IMasjid[]>;
  masjids : Array<any>;
  searchValue: string = "";
  filterMasjidName: Array<any>;

  constructor(public firebaseService: FirebaseService, 
              public af: AngularFirestore){
                this.masjids$ = this.af.collection('masjids').snapshotChanges().pipe(
                  map( actions => actions.map(masjid => {
                    let data = masjid.payload.doc.data() as IMasjid;
                    let id = masjid.payload.doc.id;
                    return { id, ...data };
                  }))
                )
              }

  searchMasjid(){
    let value = this.searchValue;
    this.firebaseService.searchMasjids(value).subscribe(
     result => {
        this.filterMasjidName = result;
        this.masjids = this.combineMasjids(result, this.filterMasjidName)
     } 
    )
  }

  combineMasjids(a, b){
    let result = [];
    a.filter(x => {
      return b.filter(x2 =>{
        if(x2.payload.doc.id == x.payload.doc.id){
          result.push(x2);
        }
      });
    });
    return result;
  }
}

service.ts: service.ts:

searchMasjids(searchValue){
    return this.db.collection('masjids.masjidInfo', 
    ref=>ref.where('name', '>=', searchValue)
    .where('name', '<=', searchValue + '\uf8ff')).snapshotChanges()
  }
}

As far as I can see, your searchMasjids method is already consuming the results, since it calls snapshotChanges() . 据我searchMasjids ,你的searchMasjids方法已经消耗了结果,因为它调用了snapshotChanges() This means that the caller of searchMasjids gets back a QuerySnapshot , while it's actually expecting a Query . 这意味着searchMasjids的调用者返回QuerySnapshot ,而它实际上期望Query

You should instead return the collection/query: 您应该返回集合/查询:

searchMasjids(searchValue){
    return this.db.collection(
      'masjids.masjidInfo', 
      ref => ref.where('name', '>=', searchValue)
                .where('name', '<=', searchValue + '\uf8ff')
    )
  }
}

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

相关问题 带有子查询的AngularFireStore返回集合 - AngularFireStore return collection with subquery AngularFirestore:检索集合中的文档引用并将 map 检索到 class - AngularFirestore: retrieving a document reference in a collection and map it to a class 通过快照更改将HTML与AngularFirestore集合绑定 - Bind HTML with AngularFirestore collection via snapshotChanges 使用AngularFirestore Collection orderBy和snapShotChanges方法 - Using AngularFirestore Collection orderBy with snapShotChanges method 使用 AngularFireStore 将文档/集合返回为 object - Return document/collection as object using AngularFireStore Angular Firebase 查询集合和子集合 - Angular Firebase Query Collection and Subcollection 模块“AppModule”导入的意外值“AngularFirestore” - Unexpected value 'AngularFirestore' imported by the module 'AppModule' 动态将集合添加到子集合中(或从子集合中获得唯一的ID?) - Add a collection into subcollection dynamically(or get the unique Id from a subcollection?) 在使用“where()”时,在AngularFirestore中查询集合时不会产生任何数据 - Querying Collection in AngularFirestore results in no data when using “where()” 无法读取collection.set()上未定义的属性afs(Angularfirestore) - Cannot read propery afs (Angularfirestore) of undefined on collection.set()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM