AngularFire2在订阅时检查列表是否为空

时间:2017-03-26 20:35:09

标签: angular firebase ionic2 angularfire2

我将AngularFire2与Ionic2应用程序一起使用。我试图从数据库中加载元素列表,并在加载时显示微调器。

当列表为空时我遇到问题,我不知道如何通知控制器停止微调器,因为在新添加之前不会有任何数据。我可能没有正确的结构。

所以这是我服务的摘录:

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);
    });
}

这是我的控制者:

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(() => {});
   });
});

如果列表为空,我的加载器永远不会被解雇(直到新的添加完成)。我想过在我的服务中对observable使用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的错误值。如果不对数据库进行额外调用,我怎么能检查我的列表是否为空? (如果可能的话)。

谢谢

1 个答案:

答案 0 :(得分:1)

基于@cartant评论,我在调用map()方法之前添加了对列表大小的检查。

const queryObservable = af.database.list('/items', {
  query: {
    orderByPriority: true,
    limitToFirst: 10,
    startAt: key,
  }
});
相关问题