【问题标题】:Firestore: sort by Firestore Timestamp causes UnhandledPromiseRejectionWarningFirestore:按 Firestore 时间戳排序导致 UnhandledPromiseRejectionWarning
【发布时间】:2019-05-01 23:41:23
【问题描述】:

我尝试从我的 Firestore 中获取文档并按时间戳对其进行排序。时间戳实际上是“firebase 格式”(见下文)

代码

我尝试使用此代码

const querySnapshot = await db
    .collection("placeVisits")
    .where("placeId", "==", req.params.placeId)
    .orderBy("time", "desc")
    .get();

但它会产生 error: (node:8356) UnhandledPromiseRejectionWarning: Error: 9 FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/...

但是,这可以正常工作(当然不能排序)

const querySnapshot = await db
    .collection("placeVisits")
    .where("placeId", "==", req.params.placeId)
    .get();

我不知道如何解决此问题或发生此问题的原因。是因为 Firestore 快照不可排序还是什么?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    .where("placeId", "==", req.params.placeId).orderBy("time", "desc") 上的查询需要一个复合索引,该索引不是由 Firestore 自动创建的。

    错误消息包含一个链接。如果您单击该链接,它会将您带到 Firebase 控制台中的页面以创建必要的索引。所有字段都应在此处预先填充,因此您只需单击一个按钮即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2020-07-31
      相关资源
      最近更新 更多