【发布时间】:2021-05-30 16:21:45
【问题描述】:
是否可以使用服务器的时间戳过滤 Firestore 查询?尝试执行以下查询时:
firebase.app.firestore()
.collection('posts')
.where('timestamp', '<=', firebase.firestore.FieldValue.serverTimestamp())
抛出以下错误。
FirebaseError: Function Query.where() called with invalid data. FieldValue.serverTimestamp() can only be used with update() and set()
我知道我可以使用new Date() 而不是FieldValue.serverTimestamp(),但如果有一种方法可以使用受信任的服务器时间戳进行查询,那就太好了。我的用例是带有到期日期的折扣代码集合,其中到期日期保存为 Firestore 时间戳。如果用户更改他们的系统时间,如果我依赖客户端日期进行过滤,则此查询可能会返回无效(过期)结果。我正在使用 Firebase 函数来实际处理折扣(使用服务器时间戳很容易验证到期),因此系统时间不正确的用户仍然无法实际使用折扣代码。尽管如此,最好保证此类查询中的时间从一开始就不会显示过期代码。
【问题讨论】:
标签: firebase google-cloud-firestore