[英]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
我过滤并在构造函数中映射有效负载数据以获取异步并访问数据库中的嵌套对象
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.