Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Filter by Date

My data:

Data

I am looking for a way to filter the dates using a custom date range. I've seen some examples online where they have queried for date, but the timestamp has always been the key which isn't possible in my case. I have tried the below code and it didn't work.

  var rootRef1 = firebase.database().ref().child("Users").orderByChild('type')
  .startAt("2019-01-05").endat("2019-01-10");


    rootRef1.on("child_added",snap => {
     var name=snap.child("fullname").val();
    var email= snap.child("email").val();
     var address= snap.child("address").val();
     var contact= snap.child("contact").val();
     var status=snap.child("status").val();
     var type=snap.child("type").val();
     var date=snap.child("regdate").val();
     console.log(name);   
   }); 

This does not seem to work. Any ideas?

like image 873
Vincent Dave Navares Te Avatar asked Jan 13 '19 17:01

Vincent Dave Navares Te


People also ask

How do you filter data in firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .

What is uf8ff firebase?

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText .

How do I get data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

Does firebase return JSON?

All Firebase Realtime Database data is stored as JSON objects. You can think of the database as a cloud-hosted JSON tree. Unlike a SQL database, there are no tables or records.


1 Answers

You're trying to filter on date values, but you're ordering on type That's not how Firebase Database queries work: you always first order on a property/value, and then filter on that same property/value.

So to order/filter on regdate:

firebase.database().ref().child("Users").orderByChild('regdate')
  .startAt("2019-01-05").endAt("2019-01-10");

Note that I also changed endat to endAt (with an uppercase A). The endat you had, will give a syntax error.


You're now ending at 2019-01-10. Since your actual values include a time, they will all fall right after this value. If you also want to include the users who registered on 2019-01-10 itself, use:

firebase.database().ref().child("Users").orderByChild('regdate')
  .startAt("2019-01-05").endAt("2019-01-10 23:59:59");
like image 167
Frank van Puffelen Avatar answered Nov 10 '22 06:11

Frank van Puffelen