2017-03-26 2 views
1

저는 AngularFire2를 Ionic2 앱과 함께 사용합니다. 데이터베이스에서 요소 목록을로드하고 로딩하는 동안 회 전자를 표시하려고합니다.AngularFire2는 구독하는 동안 목록이 비어 있는지 확인합니다.

목록이 비어있을 때 문제가 발생합니다. 새로운 추가 전에 데이터가 없기 때문에 회 전자를 중지하도록 컨트롤러에 알리는 방법을 모르겠습니다. 나는 올바른 구조가 없을 수도 있습니다. 목록이 비어

let loader = this.loadingCtrl.create({ 
    content: "Loading tours..." 
}); 
loader.present().then(() => { 
    //The list will be updated automatically 
    tourData.getExpertTourList().subscribe(tourList => { 
     this.tourList = tourList; 
     loader.dismiss().catch(() => {}); 
    }); 
}); 

경우 새로운 추가 될 때까지, 내 ​​로더 (기각하지 얻을 않을 것이다 : 이것은 내 컨트롤러

this.userTourList = this.af.database.list('/userProfiles/' + this.userId + '/tours/'); 
this.userTourList.subscribe((response) => { 
    response.map((tourData) => { 
     //Create and fill a TourSnapshot component 
     let tourSnap = new TourSnapshot(tourData.name, tourData.$key); 
     tourSnap.description = tourData.description; 
     //[..] 
     this.tours.push(tourSnap); 
     //Return an array through my own observable 
     return this.toursObservable.next(this.tours); 
    }); 
} 

:

그래서이 내 서비스의 추출물 끝난). 나는이 같은 내 서비스의 관찰에 IsEmpty 함수() 메서드를 사용하는 방법에 대한 생각 :

this.userTourList.isEmpty().subscribe(isEmpty => { 
//Force a return if the "tours" node is empty to indicate to the controller 
//that nothing else will be returned before a new add 
    if (isEmpty) { 
     return this.userToursObservable.next(this.tours); 
    } 
}); 

그러나 나는 항상 IsEmpty 함수에 대한 잘못된 가치를 얻을. 데이터베이스를 추가 호출하지 않고도 목록이 비어 있는지 확인할 수 있습니까? (그것이 가능하다면).

@cartant 의견을 바탕으로
+1

서비스에서 수행중인 작업 만 표시하려면 더 많은 코드가 필요합니다. AngularFire2'list' observables는 데이터가 없으면 빈 어레이를 내놓을 것이므로 여러분의 서비스에서 어떤 일이 일어나고 있는지 알 수 있습니다. – cartant

+0

답변 해 주셔서 감사합니다. 나는 그 질문을 갱신했다. 실제로 AngularFire2 목록은 빈 배열을 반환하므로 내 map() 메서드를 호출하기 전에 검사해야하며 빈 배열을 직접 반환해야합니다. 나는 그것을 해결책으로 게시 할 것이다. 다시 감사합니다. –

답변

0

, 내가지도() 메서드를 호출하기 전에 목록의 크기에 대한 검사를 추가 감사드립니다.

this.userTourList = this.af.database.list('/userProfiles/' + this.userId + '/tours/'); 
this.userTourList.subscribe((response) => { 
    if (response.length == 0) { 
     //Return an empty array anyway 
     return this.toursObservable.next(this.tours); 
    } 
    response.map((tourData) => { 
     //Create and fill a TourSnapshot component 
     let tourSnap = new TourSnapshot(tourData.name, tourData.$key); 
     tourSnap.description = tourData.description; 
     //[..] 
     this.tours.push(tourSnap); 
     //Return an array through my own observable 
     return this.toursObservable.next(this.tours); 
    }); 
}