我正在创建MEAN Stack应用程序,但是现在MongoDB查询(搜索功能)有一个问题...
以前,我从数据库中提取了所有用户,后端代码在此处patient.ts(您会注意到const patientQuery = Patient.find();
),并且工作正常。
前端
Patient-form-list.component.ts
ngOnInit() {
// Start the spinner
this.loading = true;
// Fetch a list of patients and the beginning
this.patientFormService.getPatients(this.patientsPerPage, this.currentPage);
...
Patient-form.service.ts
getPatients(patientsPerPage: number, currentPage: number) {
const queryParams = `?pacijenataPoStranici=${patientsPerPage}&trenutnaStranica=${currentPage}`;
this.http
.get<{ message: string, patients: any, maxPatients: number }>( BACKEND_URL + queryParams)
// Execute map on every data that makes it through Observable stream
.pipe(map((patientData) => {
return { patients: patientData.patients.map(patient => {
return {
id: patient._id,
...patient,
};
}),
maxPatients: patientData.maxPatients
};
}))
.subscribe(transformedPatientsData => {
this.patients = transformedPatientsData.patients;
this.patientsUpdated.next({
patients: [...this.patients],
patientCount: transformedPatientsData.maxPatients
});
});
}
现在,我需要动态地传递查询Patient.find()
,而不是{ phDiagnosis: { $in: [ /^ph/i ] } }
,但是我不确定该怎么做。
当我手动传递查询时,搜索功能运行良好,只有符合条件的患者才能在客户端获取...
在客户端,我将具有搜索栏,用户必须在其中填写表单,然后单击“ Pretraga”按钮,然后Patient.find()
应该动态填写,并且搜索功能应该可以工作。
<form (ngSubmit)="onSearchPatient(searchForm)" #searchForm="ngForm" *ngIf="!loading">
<div class="input-group mb-3">
<div class="input-group-prepend">
<select class="form-control criterion" (change)="criterionHasChanged($event.target.value)" [ngModel]="criterionCategory || ''" name="criterionCategory">
<option [ngValue]="''" [disabled]="true">Kriterijum pretrage</option>
<option *ngFor="let criterion of searchCriterions"
[ngValue]="criterion">{{ criterion }}
</option>
</select>
</div>
<div class="input-group-prepend" *ngIf="!loading && ages">
<select class="form-control ages rounded-0" (change)="agesHasChanged($event.target.value)" [ngModel]="ageCategory || ''" name="ageCategory">
<option [ngValue]="''" [disabled]="true">Izaberi</option>
<option *ngFor="let age of agesCriterions"
[ngValue]="age">{{ age }}
</option>
</select>
</div>
<input type="text"
class="form-control"
id="previousDeseases"
name="searchCriterion"
[ngModel]="search"
placeholder="Unesite pojam za pretragu...">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit">
Pretraga
</button>
</div>
</div>
</form>