2017-12-05 2 views
1

각도를 사용하여 firebase 데이터베이스의 데이터를 필터링하려고합니다. 오류와 같은 오류가 계속 발생합니다. TypeError : 정의되지 않은 'toLowerCase'속성을 읽을 수 없습니다. 누군가 나를 도울 수 있습니까? 아래 검색 기능이 있습니다. TSERROR TypeError : 정의되지 않은 'toLowerCase'속성을 읽을 수 없습니다. Ionic 3

searchuser(searchbar) { 


this.filteredusers = this.temparr; 
var q = searchbar.target.value; 

if (q.trim() == '') { 
    return; 
} 

this.filteredusers = this.filteredusers.filter((v) => { 
    if (v.displayName.toLowerCase().indexOf(q.toLowerCase()) > -1 || v.subject.toLowerCase().indexOf(q.toLowerCase()) > -1) { 
    return true; 
    } 

    return false; 
})} 

다음은

<ion-content padding> 
    <ion-searchbar [(ngModel)]="searchstring" (input)="searchuser($event)" placeholder="Search"></ion-searchbar> 
    <ion-list no-lines> 
     <ion-list> 
     <ion-item-sliding *ngFor="let key of filteredusers" > 
      <ion-item > 
      <ion-avatar item-left> 
       <img src="{{key.photoURL}}"> 
      </ion-avatar> 
      <h2>{{key.displayName}} </h2> 
      <p>Rate/Hour:RM{{key.rate}}</p> 
      <p>Subject:{{key.subject}}</p> 
      <p>Distance:0.5KM</p> 

      </ion-item> 


      <ion-item-options slide="left"> 
      <button ion-button color="primary" (click)="call(key)"> 
       <ion-icon name="call"></ion-icon> 
       Call 
      </button> 
      </ion-item-options> 

     </ion-item-sliding> 
     </ion-list> 
    </ion-list> 

</ion-content> 

내 필터링 된 결과가 검색 기능은 데이터베이스의 이름과 제목을 필터링 할 수 있습니다 기대 내 html 코드이다. (이 경우 toLowerCase에서) 메서드를 호출하거나 null 또는 undefined 객체에 속성에 액세스하려고하면 일반적으로 그 오류가

if (v.displayName && v.displayName.toLowerCase()... 

:에 확인 당신의 상태

+0

'v'에는 어떤 특성이 보장됩니까? 로그하면 어떻게됩니까? –

답변

0

업데이트. 본질적으로 Javascript의 버전은 null reference exception입니다. 당신이 재산 displayName이 개체 v 그 값되지 undefined 또는 null에 존재하는지 확인 v.displayName 확인함으로써

.

displayName 속성이 string이 아닌 다른 유형일 수있는 경우 toLowerCase을 호출하기 전에 속성을 유형 확인하면 도움이 될 수 있습니다. typeof v.displayName === "string"을 사용하여 유형을 확인할 수 있습니다. 따라서보다 완전한 수표는 다음과 같습니다.

if (v.displayName && typeof v.displayName === "string" && v.displayName.toLowerCase()... 
+0

당신은 내 하루 형제를 구 했어요! 지금은 일이야. 고마워요. –

+0

@akmalsharif 멋지다! 나는 그 대답을 약간 확장했다. 도움이된다면 자유롭게 투표하고 승인 된 답변으로 표시하십시오. –

관련 문제