最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Filter by Date - Stack Overflow

programmeradmin0浏览0评论

My 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?

My 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?

Share Improve this question edited Jan 13, 2019 at 17:58 Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges asked Jan 13, 2019 at 17:40 VDTeVDTe 6451 gold badge6 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

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");
发布评论

评论列表(0)

  1. 暂无评论