I’m trying to build something fairly straightforward. I am reading in a json file using an observable. This works. No problem.
I am then trying to allow a search for the content in the list. When I altered the code to make it searchable, nothing works.
If I change the ion-list to look at results, it works and displays the list. If I change it to look at information, it doesn’t work.
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button defaultHref="/"></ion-back-button>
</ion-buttons>
<ion-title>Hospitals</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-searchbar [(ngModel)]="searchTerm" (ionChange)="searchChanged()"></ion-searchbar>
<ion-list *ngIf="information">
<ion-item button *ngFor="let item of (information | async)" routerDirection="forward" [routerLink]="['/hospital-details/'+ item.id]">
<ion-label text-wrap>
<h3>{{ item.name }}</h3>
</ion-label>
</ion-item>
</ion-list>
</ion-content>
ts file
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Routes, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { GetHospitalsService } from '../../services/get-hospitals.service';
import { SearchPipe } from './../../search.pipe';
import {filter, map} from 'rxjs/operators';
@Component({
selector: 'app-hospitals',
templateUrl: './hospitals.page.html',
styleUrls: ['./hospitals.page.scss'],
})
export class HospitalsPage implements OnInit {
// information = null;
hospitals = null;
searchTerm = '';
results: Observable<any>;
information: Observable<any>;
constructor(private activatedRoute: ActivatedRoute, private hospitalService: GetHospitalsService, private router: Router) { }
ngOnInit() {
console.log('Hospitals enter');
this.results = this.hospitalService.getHospitals1();
this.information = this.filterArr2(this.searchTerm);
}
filterArr2(search): Observable<any> {
return this.results.pipe(
map(oneObj => oneObj.name.toLowerCase().indexOf(search) > -1)
);
}
searchChanged() {
const mySearch = this.searchTerm.toLowerCase();
this.information = this.filterArr2(mySearch);
}
}
Error message
ERROR TypeError: Cannot read property 'toLowerCase' of undefined
at MapSubscriber.project (hospitals.page.ts:34)
Here is a pic of what I get when I switch it to results.
If I change it to a subscribe, I get a different error, something like:
InvalidPipeArgument: ‘[object Object][object Object][object Object][object Object]’ for pipe ‘AsyncPipe’
Please, can anyone tell me the RIGHT way to do this? I’m banging my head against the desk so much I’m giving myself a concussion.