[英]Query Firestore collection by object
我创建了一个名为“books”的集合,在其中我制作了一个名为“users”的对象(又名.dict)。 该对象看起来像这样:
users: {
5PPCTcdHOtdYjGFr7gbCEsiMaWG3: firebase.firestore.FieldValue.serverTimestamp()
}
现在我想查询属于某个用户的所有书籍。 我试过这个:
this.db
.collection('books')
.where(`users.${this.state.currentUser.uid}`, '>', 0)
.onSnapshot(querySnapshot => {
...
我从来没有收到任何文件。 我确定它应该匹配。
为了检查我的理智,如果我删除where(...)
部分,我会得到文件。 只是我为所有用户提供了文档。
我控制台在.where()
记录了该字符串,它看起来正确。
我也无法使用.where()
,但似乎使用.orderBy()
,替换:
.where(`users.${this.state.currentUser.uid}`, '>', 0)
同
.orderBy(`users.${this.state.currentUser.uid}`)
您的查询是错误的,因为您正在将日期对象与数字进行比较,因此有两个不同的事情。 您可以通过在下面运行以下两行代码来检查它。
console.log(typeof(firebase.firestore.FieldValue.serverTimestamp()))
console.log(typeof(0))
所以你有两个选择:
1 - 您可以将日期对象与下面的数据对象进行比较。
this.db
.collection('books')
.where(`users.${this.state.currentUser.uid}`, '>', new Date(0))
.onSnapshot(querySnapshot => {
...
2 - 或者您可以以毫秒为单位保存日期并将其与数字进行比较。
users: {
5PPCTcdHOtdYjGFr7gbCEsiMaWG3: new Date().getTime();
}
this.db
.collection('books')
.where(`users.${this.state.currentUser.uid}`, '>', 0)
.onSnapshot(querySnapshot => {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.