根据查询选项获取firebase条目列表

时间:2017-01-31 08:59:50

标签: angular typescript firebase firebase-realtime-database angularfire2

我正在使用Angular 2来构建一个Web应用程序。我试图在子节点中满足一个或多个条件时从firebase检索数据。例如,下面是我的firebase的结构。我想挑选一个认证条目为approverEmail的条目为approver1@mail.com,状态为0(两者都必须同时满足)。

{
  "BugList" : {
    "Company 1" : {
      "-Kbne6WxelgI6Qv9T0eP" : {
        "approverEmail" : "approver1@mail.com",
        "firstName" : "Jack",
        "lastName" : "Daniels",
        "noteToApprover" : "Gooday mate",
        "status" : 0,
      },
      "-Kbne6WxelgI6Qv9T0QP" : {
        "approverEmail" : "approver2@mail.com",
        "firstName" : "Tom",
        "lastName" : "Riddle",
        "noteToApprover" : "Howdy",
        "status" : 1,
      }
    }
  },
}

目前,我使用的是angularFire2,该主题的信息量有限。这就是我现在所拥有的,而且我被困住了

forms.service.ts
getPendingApproval() {
    var currentUser: User = this.userService.getCurrentUser();
    const queryList = this.af.database.list('/BugList/' + currentUser.company, {
        query: {
            // email field match currentUser.email && status == 0 
        }
    });
}

并在我的app.component.ts

getList() {
    this.list = this.formsService.getPendingApproval()
  }

我的forms.service.ts的代码

@Injectable()
export class FormsService {

    constructor(private af: AngularFire, private userService: UserService) { }

    saveToFirebase(bug: Bug) {
        const bugRef = this.af.database.list('/BugsList/' + bug.companyName);
        return bugRef.push(timesheet)
    }


    getPendingApproval() {
        var currentUser: User = this.userService.getCurrentUser();
        const queryList = this.af.database.list('/BugsList/' + currentUser.company, {
            query: {

            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

它只能查询一个字段。 (https://github.com/angular/angularfire2/issues/305#issuecomment-230936970

您可以在收到预过滤列表后过滤第二个字段。

查询示例:

query: {
  orderByChild: 'email', // field name !
  equalTo: 'test@web.de' // search criteria
}

使用Subject

进行过滤
const filterSubject = new Subject(); // import {Subject} from 'rxjs/Subject';
const queryObservable = af.database.list('/items', {
  query: {
    orderByChild: 'email',
    equalTo: filterSubject
  }
});

// subscribe to changes
queryObservable
   .map(qis => qis.filter(q => q.status == 0)) // FILTER HERE FOR STATUS
   .subscribe(queriedItems => {
      console.log(queriedItems);
   });

// trigger the query
filterSubject.next('test@web.de');

// re-trigger the query!!!
filterSubject.next('huhu@gmail.com');

docs:https://github.com/angular/angularfire2/blob/master/docs/4-querying-lists.md