user172902
user172902

Reputation: 3581

Get list of firebase entry based on query option

I am using Angular 2 to build a web app. I am trying to retrieve data from firebase when one or more of the condition(s) are met in the child node. For example, below is how my firebase are structured. I want to pick out the entry for the one with approverEmail being [email protected] as well as status being 0 (Both have to be met at the same time).

{
  "BugList" : {
    "Company 1" : {
      "-Kbne6WxelgI6Qv9T0eP" : {
        "approverEmail" : "[email protected]",
        "firstName" : "Jack",
        "lastName" : "Daniels",
        "noteToApprover" : "Gooday mate",
        "status" : 0,
      },
      "-Kbne6WxelgI6Qv9T0QP" : {
        "approverEmail" : "[email protected]",
        "firstName" : "Tom",
        "lastName" : "Riddle",
        "noteToApprover" : "Howdy",
        "status" : 1,
      }
    }
  },
}

Currently, I am using angularFire2 which has limited amount of information on this topic. This is what I have at the moment and I am stuck

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

and in my app.component.ts

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

Code for my 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: {

            }
        });
    }
}

Upvotes: 0

Views: 2261

Answers (1)

slaesh
slaesh

Reputation: 16917

Its only possible to query one field. (https://github.com/angular/angularfire2/issues/305#issuecomment-230936970)

You could filter the second field after receiving the pre-filtered list.

Example for a query:

query: {
  orderByChild: 'email', // field name !
  equalTo: '[email protected]' // search criteria
}

Filtering using a 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('[email protected]');

// re-trigger the query!!!
filterSubject.next('[email protected]');

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

Upvotes: 1

Related Questions