Firestore orderBy 和 where 过滤器不能一起工作

Firestore orderBy and where filters not working together

如果我查询我的 users 集合并按 2 个不同的属性进行过滤,如下所示,它 returns 预期数据。

return this.afs
  .collection<Employee>('users', ref => {
    return ref
      .where('accountId', '==', accountId)
      .where('isEmployee', '==', true);
  })
  .valueChanges()

但是,如果我尝试按 name 对数据进行排序,如下所示,它 returns 是一个空数组。

return this.afs
  .collection<Employee>('users', ref => {
    return ref
      .where('accountId', '==', accountId)
      .where('isEmployee', '==', true)
      .orderBy('name');
  })
  .valueChanges()

我想我必须需要管理索引的处理方式或其他东西,但做了一些研究,我的理解是如果是这样的话,我会在控制台中收到一条错误消息 link 来设置我需要的复合索引。我没有收到任何错误,只是一个空数组。我究竟做错了什么?是否可以订购由 2 个属性过滤的数据集合?我相信我使用的是 Firebase 6.5 版,但如果能解决我的问题,我愿意更新。

您似乎还没有准备好捕捉错误。可能想做类似的事情:

.catch(错误=> console.log(错误)

(将在桌面上格式化)

I would get an error message in the console with a link to setup the composite indexing that I need.

是的,没错。当您使用像您这样的查询时,控制台中应该会显示一条警告消息。

I don't get any errors though, just an empty array.

在这种情况下,您应该在 Firebase console 中手动创建该索引。

Is it possible to order a collection of data that is filtered by 2 properties?

当然是。只需创建所需的索引,然后您就可以执行查询了。