我已经在使用Firebase的angular 7应用程序中实现了搜索功能。我希望用户始终提供from和to值以及可选的日期值。现在,只要用户提供所有3个值(从,到&日期),查询就会返回预期结果。但是,如果仅提供从&到值,则查询有时会返回预期结果,而在其他时候则不会。我无法在代码中找到错误,因此我需要你们帮助查看我的代码,看看我是否做错了什么。这是我的代码
schedule.service.ts
searchSchedulesList(route: string, date) {
let now = this.formatDate(this.today);
if (date != null) {
return this.afs
.collection("schedules", ref =>
ref
.where("route", "==", route)
.where("date", "==", date)
.where("date", ">=", now)
.orderBy("date", "asc")
.orderBy("time", "asc")
)
.snapshotChanges();
} else {
return this.afs
.collection("schedules", ref => ref.where("route", "==", route))
.snapshotChanges();
}
}
schedule.component.ts
search(route: string, date) {
this.scheduleService.searchSchedulesList(route, date).subscribe(data => {
this.schedules = data.map(e => {
return {
id: e.payload.doc.id,
...e.payload.doc.data()
} as Schedule;
});
});
}
这是用于获取值from
to
和date
<form [formGroup]="searchForm">
<div class="row">
<div class="form-group col-3">
<select formControlName="from" id="from" class="form-control">
<option value="">From</option>
<option
*ngFor="let destination of destinations"
value="{{ destination.abbreviation }}"
>{{ destination.fullname }}</option
>
</select>
</div>
<div class="form-group col-3">
<select formControlName="to" id="to" class="form-control">
<option value="">To</option>
<option
*ngFor="let destination of destinations"
value="{{ destination.abbreviation }}"
>{{ destination.fullname }}</option
>
</select>
</div>
<div class="form-group col-3">
<input formControlName="date" type="date" class="form-control" />
</div>
<div class="col-3">
<button
class="btn btn-primary btn-sm"
(click)="
search(
searchForm.value.from + searchForm.value.to,
searchForm.value.date
)
"
>
Search
</button>
<button class="btn btn-secondary btn-sm" (click)="clear()">
Reset
</button>
</div>
</div>
</form>