【问题标题】:FireStore Multiple where queryFireStore 多个 where 查询
【发布时间】:2019-04-05 05:58:40
【问题描述】:

我在下面有以下 Firestore 查询。我正在尝试执行多个 在哪里查询 Book 集合。我想按书名和书年龄范围过滤。但是我收到以下错误

“onsnapshot firebaseError 中未捕获的错误:光标位置超出了原始查询的范围”有人可以建议。

           const collectionRef = firebase.firestore().collection('Books')

           collectionRef.where('d.details.BookType',"==",BookType)
           collectionRef = collectionRef.where('d.details.bookage',"<=",age)
           collectionRef = collectionRef.orderBy('d.details.bookage')


             const geoFirestore = new GeoFirestore(collectionRef)

              const geoQuery = geoFirestore.query({
              center: new firebase.firestore.GeoPoint(lat, long),
              radius: val,

              });

            geoQuery.on("key_entered",function(key, coords, distance) {   

storeCoordinate(key,coords.coordinates._lat,coords.coordinates._long,newdata)
});

【问题讨论】:

  • 您是否尝试过使用const geoFirestore = new GeoFirestore(collectionRef.where('d.details.BookType',"==",BookType).where('d.details.bookage',"&lt;=",age).orderBy('d.details.bookage')) 之类的链式调用来代替const geoFirestore = new GeoFirestore(collectionRef)
  • 我刚试过,但它不起作用我不确定问题是什么。
  • 你的行为是否与上述解决方案相同?
  • 是的,错误消息与初始错误相同。你能复制吗?
  • 我相信 orderBy 语法会导致问题,但是我确实需要 order by,因为我正在对书龄进行过滤。

标签: react-native google-cloud-firestore


【解决方案1】:

geoFirestore 在内部通过以下方式获取结果 this._query.orderBy('g').startAt(query[0]).endAt(query[1])

按顺序排列,扩展您的collectionRef,正在发生这样的事情:

const collectionRef = firebase.firestore().collection('Books')
collectionRef.where('d.details.BookType',"==",BookType)
collectionRef = collectionRef.where('d.details.bookage',"<=",age)
collectionRef = collectionRef.orderBy('d.details.bookage')
collectionRef.orderBy('g').startAt(query[0]).endAt(query[1])

出现问题是因为.startAt 指的是您的第一个orderBy,即d.details.bookage,所以它是从光标处开始 d.details.bookage query[0].

看到query[0] 是一个geohash,它转换为类似从光标处开始 d.details.bookage w2838p5j0smt,因此出现错误。 p>


解决方案

有两种方法可以解决此限制。

  1. 等待 Geofirestore 的更新,我认为 @MichaelSolati 已经在努力了。
  2. 从 Geofirestore 的onKey 获取结果后对结果进行排序

【讨论】:

  • 很遗憾,我无法更新库来解决这个问题。但我确实还有其他需要解决的问题(我只需要在 v3 发布之前为库编写测试)
  • @MichaelSolati 我看到您正在努力让用户获得结果的快照,而无需在onKeyEntered 中汇总它们,我认为这对第 2 步很有帮助。期待 v3。
猜你喜欢
  • 2020-08-01
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 2021-05-13
  • 2018-05-18
  • 2023-03-09
相关资源
最近更新 更多