【问题标题】:Firestore sort by valueFirestore 按值排序
【发布时间】:2021-11-16 19:20:33
【问题描述】:

我正在尝试为我的应用程序创建一个过滤器 它将按数据的类别进行过滤,但我也想按其评级对它们进行排序,较高的评级将位于顶部。 我写了以下代码,但是 orderby() 函数有问题。

        const snapshot = await firebase.firestore().collection('Locations').where(
            "category", "==", type).orderBy("rating").get();
        
        if (snapshot.empty){
            console.log('No matching documents');
            return;
        }
        
        snapshot.forEach(doc=> {
            locList.push(doc.data());
        })

这是我的 Firestore 数据的样子

【问题讨论】:

  • 你得到什么回应?

标签: javascript firebase google-cloud-firestore


【解决方案1】:

要将值从最大评级到最小评级排序,请将排序指定为降序。将orderBy 函数更改为:

.orderBy('rating', 'desc')

【讨论】:

  • 我收到[Unhandled promise rejection: FirebaseError: The query requires an index. You can create it here:...的错误
  • 您需要在 firebase 控制台上创建索引。按照显示的链接(在...之后),你应该很好。
  • 我正在手动创建索引,但是这两个数据应该是“升序”还是“降序”或“数组”?查询范围也应该是“集合”还是“集合组”?
  • 集合 ID:位置。字段: - 类别 - 升序评级 - 降序。查询范围:“收藏”。
  • @AdamNeuwirth 嗨亚当,如果我需要从最小到最大怎么办?将 desc 更改为 asce?
猜你喜欢
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 2021-01-05
相关资源
最近更新 更多