0

In my Firebase I have a entry called 'status' which has some records as 'active' and others as 'inactive'. I am trying to return all records that have the status of 'active'. I am doing the following statement.

new Firebase($rootScope.constants.firebase)
  .startAt('active')
  .endAt('active')
  .once('value', function(snap){
    console.log(snap.val());
  });

However this is returning NULL.

I am adding data to firebase in the following way:

$firebase(ref).$add({
  date: Firebase.ServerValue.TIMESTAMP,
  status: 'active'
})

My firebase data looks like the following:

{ "-JSex4_si4a2ZMOLcEuj" : {
  "date" : 1406248704619,
  "status" : "active",
},
"-JShWyGQzDtAl-F8EkOI" : {
  "date" : 1406291928576,
  "status" : "active",
}}

Please can someone explain why I am getting NULL and how to get all records with this 'active' status?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Peter I
  • 843
  • 1
  • 9
  • 31
  • You can only filter by **name** and **priority** in Firebase, there is no generic, SQL like WHERE clause. If you set your status as the priority of each node, you can filter on that. But remember: each node can have only one name and one priority. Anything beyond that will require an alternate data structure. Search for "firebase indexes" for more on that. – Frank van Puffelen Jul 27 '14 at 01:34
  • 1
    See http://stackoverflow.com/questions/24869180/ for somebody who recently had a similar problem: trying to filter on another property than name or priority. It simply isn't a feature in Firebase. But most often you can work around it by changing your data structure as the user in the linked question shows. – Frank van Puffelen Jul 27 '14 at 21:09
  • So to extend on Frank's point, you'll want to use setWithPriority and set the priority of the record to the same value as "status" if you want to query based on that. See also this post for some more advanced ideas on querying: http://stackoverflow.com/questions/25034860/firebase-how-to-match-opponents-in-a-game/25039133#25039133 – Kato Jul 30 '14 at 15:03

0 Answers0