0

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 approver1@mail.com as well as status being 0 (Both have to be met at the same time).

{
  "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,
      }
    }
  },
}

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: {

            }
        });
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user172902
  • 3,541
  • 9
  • 32
  • 75

1 Answers1

1

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: 'test@web.de' // 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('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

slaesh
  • 16,659
  • 6
  • 50
  • 52
  • 1
    Since this is the accepted answer, also have a look at http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase, which covers a way to do server-side filtering on two values (yet still only one property) in some cases. – Frank van Puffelen Jan 31 '17 at 14:53
  • That's `pure firebase`, not the required `angularfire2`. But good to know! :) – slaesh Jan 31 '17 at 15:00
  • 1
    AngularFire2 is a wrapper around the Firebase JavaScript SDK. The capabilities when it comes to querying multiple values are the same: if you can combine the values into a single property (in the way described in the linked answer), you can filter on them with AngularFire2 too.. – Frank van Puffelen Jan 31 '17 at 15:14