Firebase按时间筛选未来数据失败,寻求解决方法
解决Firestore查询未来Timestamp数据的问题
你的问题出在没有用Firestore的Timestamp类型做对比,而是用了普通Date或者直接访问Timestamp的内部字段,Firestore不支持这种查询方式。
问题分析
- 你数据库里的
startTime是Firestore的Timestamp类型(包含seconds和nanoseconds属性),这类特殊类型不能直接用普通Date对象或者其内部字段做查询条件。 - 直接访问
startTime.seconds的写法无效,因为Firestore不允许针对Timestamp的嵌套属性构建查询。
正确写法
你需要用Firestore提供的Timestamp类来构建查询条件,有两种常用方式:
方式1:直接获取当前时间的Timestamp
ref.where("startTime", ">", firebase.firestore.Timestamp.now())
方式2:将普通Date转成Timestamp
如果你需要基于某个特定Date对象查询:
const targetDate = new Date(); // 这里可以替换成你需要的时间 ref.where("startTime", ">", firebase.firestore.Timestamp.fromDate(targetDate))
额外检查
确认你的startTime字段在Firestore控制台里确实是Timestamp类型,而不是字符串或其他类型——如果是字符串,你需要先统一字段类型,否则上述写法仍然无效。
内容的提问来源于stack exchange,提问作者Bayrsaihan Tergeltengis
相关产品推荐
相关产品推荐

