Firebase按日期筛选 [英] Firebase Filter by Date
问题描述
我的数据:
我正在寻找一种使用自定义日期范围过滤日期的方法. 我在网上看到了一些查询日期的示例, 但是时间戳一直是我无法解决的关键. 我已经尝试了下面的代码,但是没有用.
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?
推荐答案
您尝试按日期值进行过滤,但您要按type
进行排序.这不是Firebase数据库查询的工作方式:您始终首先对属性/值,然后根据该属性/值进行过滤.
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.
因此要在regdate
上订购/过滤:
So to order/filter on regdate
:
firebase.database().ref().child("Users").orderByChild('regdate')
.startAt("2019-01-05").endAt("2019-01-10");
请注意,我也将endat
更改为endAt
(使用大写的A
).您使用的endat
会出现语法错误.
Note that I also changed endat
to endAt
(with an uppercase A
). The endat
you had, will give a syntax error.
您现在以2019-01-10
结尾.由于您的实际值包含一个时间,因此它们都将落在该值之后.如果您还想包括在2019-01-10
本身上注册的用户,请使用:
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");
这篇关于Firebase按日期筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!